From a2911be37e5483aeba81b3fa9ab39d9b0d0aa836 Mon Sep 17 00:00:00 2001 From: 18392181980 Date: Tue, 28 Sep 2021 15:52:37 +0800 Subject: [PATCH] usb_service_commit_0928 --- LICENSE | 177 ++ hdi/BUILD.gn | 21 + hdi/api/include/usbd_api.h | 252 ++ hdi/client/BUILD.gn | 49 + hdi/client/include/readme.md | 27 + hdi/client/include/usbd_client.h | 128 + hdi/client/include/usbd_subscriber.h | 46 + hdi/client/src/usbd_client.cpp | 718 +++++ hdi/client/src/usbd_subscriber.cpp | 106 + hdi/service/BUILD.gn | 85 + hdi/service/include/usbd.h | 100 + hdi/service/include/usbd_dispatcher.h | 22 + hdi/service/include/usbd_function.h | 7 + hdi/service/include/usbd_parser.h | 37 + hdi/service/include/usbd_port.h | 9 + hdi/service/include/usbd_publisher.h | 31 + hdi/service/src/usbd.c | 394 +++ hdi/service/src/usbd_dispatcher.c | 2020 ++++++++++++++ hdi/service/src/usbd_function.c | 269 ++ hdi/service/src/usbd_port.c | 144 + hdi/service/src/usbd_publisher.c | 85 + hdi/test/BUILD.gn | 22 + hdi/test/unittest/common/BUILD.gn | 293 ++ .../common/include/usbd_config_test.h | 27 + .../common/include/usbd_descriptor_test.h | 27 + .../common/include/usbd_device_test.h | 27 + .../common/include/usbd_function_test.h | 27 + .../common/include/usbd_interface_test.h | 27 + .../unittest/common/include/usbd_port_test.h | 27 + .../common/include/usbd_request_test.h | 27 + .../common/include/usbd_transfer_test.h | 27 + .../unittest/common/src/usbd_config_test.cpp | 488 ++++ .../common/src/usbd_config_test.cpp.bak | 493 ++++ .../common/src/usbd_descriptor_test.cpp | 928 +++++++ .../unittest/common/src/usbd_device_test.cpp | 237 ++ .../common/src/usbd_function_test.cpp | 97 + .../common/src/usbd_interface_test.cpp | 576 ++++ .../unittest/common/src/usbd_port_test.cpp | 246 ++ .../unittest/common/src/usbd_request_test.cpp | 721 +++++ .../common/src/usbd_transfer_test.cpp | 2349 +++++++++++++++++ interfaces/innerkits/BUILD.gn | 55 + .../innerkits/native/include/iusb_srv.h | 93 + .../innerkits/native/include/usb_config.h | 140 + .../innerkits/native/include/usb_device.h | 186 ++ .../innerkits/native/include/usb_endpoint.h | 125 + .../innerkits/native/include/usb_info.h | 96 + .../innerkits/native/include/usb_interface.h | 134 + .../innerkits/native/include/usb_pipe.h | 63 + .../innerkits/native/include/usb_port.h | 76 + .../native/include/usb_port_status.h | 53 + .../innerkits/native/include/usb_request.h | 78 + .../innerkits/native/include/usb_srv_client.h | 100 + .../innerkits/native/src/usb_device.cpp | 42 + interfaces/innerkits/native/src/usb_pipe.cpp | 117 + .../innerkits/native/src/usb_request.cpp | 55 + .../innerkits/native/src/usb_srv_client.cpp | 376 +++ interfaces/kits/java/BUILD.gn | 54 + .../ohos/usb/IUSBDevicePipeController.java | 42 + .../java/ohos/usb/IUSBRequestController.java | 28 + .../java/ohos/usb/IUsbCoreController.java | 43 + interfaces/kits/java/ohos/usb/USBConfig.java | 143 + interfaces/kits/java/ohos/usb/USBCore.java | 191 ++ interfaces/kits/java/ohos/usb/USBDevice.java | 290 ++ .../kits/java/ohos/usb/USBDevicePipe.java | 263 ++ .../ohos/usb/USBDevicePipeController.java | 142 + .../kits/java/ohos/usb/USBEndpoint.java | 151 ++ .../kits/java/ohos/usb/USBInterface.java | 172 ++ .../kits/java/ohos/usb/USBLoggerFactory.java | 46 + interfaces/kits/java/ohos/usb/USBPort.java | 4 + interfaces/kits/java/ohos/usb/USBRequest.java | 131 + .../kits/java/ohos/usb/UsbCoreController.java | 142 + .../java/ohos/usb/UsbCoreControllerProxy.java | 414 +++ .../usb/UsbDevicePipeControllerProxy.java | 173 ++ .../kits/java/ohos/usb/UsbKitsUtils.java | 7 + .../kits/java/ohos/usb/UsbPortStatus.java | 4 + .../java/ohos/usb/UsbRequestController.java | 142 + .../ohos/usb/UsbRequestControllerProxy.java | 134 + .../kits/java/ohos/usb/package-info.java | 24 + interfaces/kits/js/@ohos.usb.d.ts | 879 ++++++ interfaces/kits/js/napi/BUILD.gn | 48 + interfaces/kits/js/napi/usb_async_context.h | 75 + interfaces/kits/js/napi/usb_info.cpp | 1728 ++++++++++++ interfaces/kits/js/test/usb_unit.test.js | 495 ++++ ohos.build | 35 + sa_profile/4201.xml | 24 + sa_profile/BUILD.gn | 19 + services/BUILD.gn | 176 ++ .../native/include/usb_descriptor_parser.h | 78 + services/native/include/usb_device_manager.h | 21 + .../native/include/usb_function_manager.h | 40 + services/native/include/usb_host_manager.h | 57 + .../native/include/usb_permission_manager.h | 38 + services/native/include/usb_port_manager.h | 40 + services/native/include/usb_serial_reader.h | 30 + .../native/include/usb_server_event_handler.h | 41 + services/native/include/usb_service.h | 110 + .../native/include/usb_service_subscriber.h | 38 + services/native/src/usb_descriptor_parser.cpp | 285 ++ services/native/src/usb_device_manager.cpp | 29 + services/native/src/usb_function_manager.cpp | 76 + services/native/src/usb_function_test.cpp | 58 + services/native/src/usb_host_manager.cpp | 155 ++ .../native/src/usb_permission_manager.cpp | 65 + services/native/src/usb_port_manager.cpp | 118 + services/native/src/usb_port_test.cpp | 109 + services/native/src/usb_serial_reader.cpp | 21 + .../native/src/usb_server_event_handler.cpp | 39 + services/native/src/usb_service.cpp | 496 ++++ .../native/src/usb_service_subscriber.cpp | 113 + services/native/src/usb_test.cpp | 543 ++++ services/native/test/BUILD.gn | 22 + services/native/test/unittest/common/BUILD.gn | 166 ++ .../common/include/usb_controltransfer_test.h | 28 + .../unittest/common/include/usb_core_test.h | 13 + .../common/include/usb_device_pipe_test.h | 13 + .../include/usb_function_service_test.h | 28 + .../unittest/common/include/usb_pipe_test.h | 28 + .../common/include/usb_request_test.h | 28 + .../common/include/usb_service_test.h | 29 + .../common/src/usb_controltransfer_test.cpp | 460 ++++ .../unittest/common/src/usb_core_test.cpp | 483 ++++ .../common/src/usb_device_pipe_test.cpp | 286 ++ .../common/src/usb_function_service_test.cpp | 119 + .../unittest/common/src/usb_pipe_test.cpp | 1504 +++++++++++ .../common/src/usb_port_service_test.cpp | 210 ++ .../unittest/common/src/usb_request_test.cpp | 623 +++++ .../unittest/common/src/usb_service_test.cpp | 59 + services/zidl/include/usb_server_proxy.h | 79 + services/zidl/include/usb_server_stub.h | 88 + services/zidl/iusb_server.zidl | 19 + services/zidl/src/usb_srv_proxy.cpp | 840 ++++++ services/zidl/src/usb_srv_stub.cpp | 656 +++++ test/native/BUILD.gn | 20 + test/native/unittest/BUILD.gn | 72 + .../unittest/include/usb_service_test.h | 26 + test/native/unittest/src/usb_service_test.cpp | 226 ++ usbmgr.gni | 18 + utils/BUILD.gn | 21 + utils/native/include/hilog_wrapper.h | 108 + utils/native/include/sp_singleton.h | 77 + utils/native/include/usb_common.h | 138 + utils/native/include/usb_errors.h | 48 + 142 files changed, 28536 insertions(+) create mode 100644 LICENSE create mode 100644 hdi/BUILD.gn create mode 100644 hdi/api/include/usbd_api.h create mode 100644 hdi/client/BUILD.gn create mode 100644 hdi/client/include/readme.md create mode 100644 hdi/client/include/usbd_client.h create mode 100644 hdi/client/include/usbd_subscriber.h create mode 100644 hdi/client/src/usbd_client.cpp create mode 100644 hdi/client/src/usbd_subscriber.cpp create mode 100644 hdi/service/BUILD.gn create mode 100644 hdi/service/include/usbd.h create mode 100644 hdi/service/include/usbd_dispatcher.h create mode 100644 hdi/service/include/usbd_function.h create mode 100644 hdi/service/include/usbd_parser.h create mode 100644 hdi/service/include/usbd_port.h create mode 100644 hdi/service/include/usbd_publisher.h create mode 100644 hdi/service/src/usbd.c create mode 100644 hdi/service/src/usbd_dispatcher.c create mode 100644 hdi/service/src/usbd_function.c create mode 100644 hdi/service/src/usbd_port.c create mode 100644 hdi/service/src/usbd_publisher.c create mode 100644 hdi/test/BUILD.gn create mode 100644 hdi/test/unittest/common/BUILD.gn create mode 100644 hdi/test/unittest/common/include/usbd_config_test.h create mode 100644 hdi/test/unittest/common/include/usbd_descriptor_test.h create mode 100644 hdi/test/unittest/common/include/usbd_device_test.h create mode 100644 hdi/test/unittest/common/include/usbd_function_test.h create mode 100644 hdi/test/unittest/common/include/usbd_interface_test.h create mode 100644 hdi/test/unittest/common/include/usbd_port_test.h create mode 100644 hdi/test/unittest/common/include/usbd_request_test.h create mode 100644 hdi/test/unittest/common/include/usbd_transfer_test.h create mode 100644 hdi/test/unittest/common/src/usbd_config_test.cpp create mode 100644 hdi/test/unittest/common/src/usbd_config_test.cpp.bak create mode 100644 hdi/test/unittest/common/src/usbd_descriptor_test.cpp create mode 100644 hdi/test/unittest/common/src/usbd_device_test.cpp create mode 100644 hdi/test/unittest/common/src/usbd_function_test.cpp create mode 100644 hdi/test/unittest/common/src/usbd_interface_test.cpp create mode 100644 hdi/test/unittest/common/src/usbd_port_test.cpp create mode 100644 hdi/test/unittest/common/src/usbd_request_test.cpp create mode 100644 hdi/test/unittest/common/src/usbd_transfer_test.cpp create mode 100644 interfaces/innerkits/BUILD.gn create mode 100644 interfaces/innerkits/native/include/iusb_srv.h create mode 100644 interfaces/innerkits/native/include/usb_config.h create mode 100644 interfaces/innerkits/native/include/usb_device.h create mode 100644 interfaces/innerkits/native/include/usb_endpoint.h create mode 100644 interfaces/innerkits/native/include/usb_info.h create mode 100644 interfaces/innerkits/native/include/usb_interface.h create mode 100644 interfaces/innerkits/native/include/usb_pipe.h create mode 100644 interfaces/innerkits/native/include/usb_port.h create mode 100644 interfaces/innerkits/native/include/usb_port_status.h create mode 100644 interfaces/innerkits/native/include/usb_request.h create mode 100644 interfaces/innerkits/native/include/usb_srv_client.h create mode 100644 interfaces/innerkits/native/src/usb_device.cpp create mode 100644 interfaces/innerkits/native/src/usb_pipe.cpp create mode 100644 interfaces/innerkits/native/src/usb_request.cpp create mode 100644 interfaces/innerkits/native/src/usb_srv_client.cpp create mode 100644 interfaces/kits/java/BUILD.gn create mode 100644 interfaces/kits/java/ohos/usb/IUSBDevicePipeController.java create mode 100644 interfaces/kits/java/ohos/usb/IUSBRequestController.java create mode 100644 interfaces/kits/java/ohos/usb/IUsbCoreController.java create mode 100644 interfaces/kits/java/ohos/usb/USBConfig.java create mode 100644 interfaces/kits/java/ohos/usb/USBCore.java create mode 100644 interfaces/kits/java/ohos/usb/USBDevice.java create mode 100644 interfaces/kits/java/ohos/usb/USBDevicePipe.java create mode 100644 interfaces/kits/java/ohos/usb/USBDevicePipeController.java create mode 100644 interfaces/kits/java/ohos/usb/USBEndpoint.java create mode 100644 interfaces/kits/java/ohos/usb/USBInterface.java create mode 100644 interfaces/kits/java/ohos/usb/USBLoggerFactory.java create mode 100644 interfaces/kits/java/ohos/usb/USBPort.java create mode 100644 interfaces/kits/java/ohos/usb/USBRequest.java create mode 100644 interfaces/kits/java/ohos/usb/UsbCoreController.java create mode 100644 interfaces/kits/java/ohos/usb/UsbCoreControllerProxy.java create mode 100644 interfaces/kits/java/ohos/usb/UsbDevicePipeControllerProxy.java create mode 100644 interfaces/kits/java/ohos/usb/UsbKitsUtils.java create mode 100644 interfaces/kits/java/ohos/usb/UsbPortStatus.java create mode 100644 interfaces/kits/java/ohos/usb/UsbRequestController.java create mode 100644 interfaces/kits/java/ohos/usb/UsbRequestControllerProxy.java create mode 100644 interfaces/kits/java/ohos/usb/package-info.java create mode 100644 interfaces/kits/js/@ohos.usb.d.ts create mode 100644 interfaces/kits/js/napi/BUILD.gn create mode 100644 interfaces/kits/js/napi/usb_async_context.h create mode 100644 interfaces/kits/js/napi/usb_info.cpp create mode 100644 interfaces/kits/js/test/usb_unit.test.js create mode 100644 ohos.build create mode 100644 sa_profile/4201.xml create mode 100644 sa_profile/BUILD.gn create mode 100644 services/BUILD.gn create mode 100644 services/native/include/usb_descriptor_parser.h create mode 100644 services/native/include/usb_device_manager.h create mode 100644 services/native/include/usb_function_manager.h create mode 100644 services/native/include/usb_host_manager.h create mode 100644 services/native/include/usb_permission_manager.h create mode 100644 services/native/include/usb_port_manager.h create mode 100644 services/native/include/usb_serial_reader.h create mode 100644 services/native/include/usb_server_event_handler.h create mode 100644 services/native/include/usb_service.h create mode 100644 services/native/include/usb_service_subscriber.h create mode 100644 services/native/src/usb_descriptor_parser.cpp create mode 100644 services/native/src/usb_device_manager.cpp create mode 100644 services/native/src/usb_function_manager.cpp create mode 100644 services/native/src/usb_function_test.cpp create mode 100644 services/native/src/usb_host_manager.cpp create mode 100644 services/native/src/usb_permission_manager.cpp create mode 100644 services/native/src/usb_port_manager.cpp create mode 100644 services/native/src/usb_port_test.cpp create mode 100644 services/native/src/usb_serial_reader.cpp create mode 100644 services/native/src/usb_server_event_handler.cpp create mode 100644 services/native/src/usb_service.cpp create mode 100644 services/native/src/usb_service_subscriber.cpp create mode 100644 services/native/src/usb_test.cpp create mode 100644 services/native/test/BUILD.gn create mode 100644 services/native/test/unittest/common/BUILD.gn create mode 100644 services/native/test/unittest/common/include/usb_controltransfer_test.h create mode 100644 services/native/test/unittest/common/include/usb_core_test.h create mode 100644 services/native/test/unittest/common/include/usb_device_pipe_test.h create mode 100644 services/native/test/unittest/common/include/usb_function_service_test.h create mode 100644 services/native/test/unittest/common/include/usb_pipe_test.h create mode 100644 services/native/test/unittest/common/include/usb_request_test.h create mode 100644 services/native/test/unittest/common/include/usb_service_test.h create mode 100644 services/native/test/unittest/common/src/usb_controltransfer_test.cpp create mode 100644 services/native/test/unittest/common/src/usb_core_test.cpp create mode 100644 services/native/test/unittest/common/src/usb_device_pipe_test.cpp create mode 100644 services/native/test/unittest/common/src/usb_function_service_test.cpp create mode 100644 services/native/test/unittest/common/src/usb_pipe_test.cpp create mode 100644 services/native/test/unittest/common/src/usb_port_service_test.cpp create mode 100644 services/native/test/unittest/common/src/usb_request_test.cpp create mode 100644 services/native/test/unittest/common/src/usb_service_test.cpp create mode 100644 services/zidl/include/usb_server_proxy.h create mode 100644 services/zidl/include/usb_server_stub.h create mode 100644 services/zidl/iusb_server.zidl create mode 100644 services/zidl/src/usb_srv_proxy.cpp create mode 100644 services/zidl/src/usb_srv_stub.cpp create mode 100644 test/native/BUILD.gn create mode 100644 test/native/unittest/BUILD.gn create mode 100644 test/native/unittest/include/usb_service_test.h create mode 100644 test/native/unittest/src/usb_service_test.cpp create mode 100644 usbmgr.gni create mode 100644 utils/BUILD.gn create mode 100644 utils/native/include/hilog_wrapper.h create mode 100644 utils/native/include/sp_singleton.h create mode 100644 utils/native/include/usb_common.h create mode 100644 utils/native/include/usb_errors.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..4947287f --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/hdi/BUILD.gn b/hdi/BUILD.gn new file mode 100644 index 00000000..e2b675c4 --- /dev/null +++ b/hdi/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2021 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. + +import("//build/ohos.gni") + +group("hdi_group") { + deps = [ + "client:usbd_client", + "service:usbd" + ] +} diff --git a/hdi/api/include/usbd_api.h b/hdi/api/include/usbd_api.h new file mode 100644 index 00000000..de937521 --- /dev/null +++ b/hdi/api/include/usbd_api.h @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBD_API_H +#define USBD_API_H + +#include +#include + +#define USB_MAX_INTERFACES 32 +typedef void (*UsbdRequestCallback)(void *requestArg); + +enum UsbdDeviceAction{ + ACT_DEVUP = 0, + ACT_DEVDOWN, + ACT_UPDEVICE, + ACT_DOWNDEVICE, +}; + +enum UsbdCmd { + CMD_BIND_USB_SUBSCRIBER = 0, + CMD_UNBIND_USB_SUBSCRIBER, + CMD_NOTIFY_DEVICE_DOWN, + CMD_NOTIFY_DEVICE_FOUND, + CMD_NOTIFY_PORT_CHANGED, + CMD_CLASS_CTRL_SYNC, + CMD_FUN_ADD_FUNCTION, + CMD_FUN_ADD_INTERFACE, + CMD_FUN_CLAIM_INTERFACE, + CMD_FUN_CLOSE_DEVICE, + CMD_FUN_CONNECT_DEVICE, + CMD_FUN_DELETE_INTERFACE, + CMD_FUN_GET_CONFIG, + CMD_FUN_GET_CURRENT_FUNCTIONS, + CMD_FUN_GET_DESCRIPTOR, + CMD_FUN_GET_DEVICE_DESCRIPTOR, + CMD_FUN_GET_CONFIG_DESCRIPTOR, + CMD_FUN_GET_STRING_DESCRIPTOR, + CMD_FUN_GET_INTERFACE_DESCRIPTOR, + CMD_FUN_GET_ENDPOINT_DESCRIPTOR, + CMD_FUN_HANDLE_REQUEST, + CMD_FUN_OPEN_DEVICE, + CMD_FUN_RELEASE_INTERFACE, + CMD_FUN_REMOVE_FUNCTION, + CMD_FUN_REMOVE_INTERFACE, + CMD_FUN_SEND_BULK_READ_ASYNC, + CMD_FUN_SEND_BULK_READ_SYNC, + CMD_FUN_SEND_BULK_WRITE_ASYNC, + CMD_FUN_SEND_BULK_WRITE_SYNC, + CMD_FUN_SEND_CTRL_READ_ASYNC, + CMD_FUN_SEND_CTRL_READ_SYNC, + CMD_FUN_SEND_CTRL_REQUEST_ASYNC, + CMD_FUN_SEND_CTRL_REQUEST_SYNC, + CMD_FUN_SEND_CTRL_WRITE_ASYNC, + CMD_FUN_SEND_CTRL_WRITE_SYNC, + CMD_FUN_SEND_INTERRUPT_READ_ASYNC, + CMD_FUN_SEND_INTERRUPT_READ_SYNC, + CMD_FUN_SEND_INTERRUPT_WRITE_ASYNC, + CMD_FUN_SEND_INTERRUPT_WRITE_SYNC, + CMD_FUN_SEND_ISO_READ_ASYNC, + CMD_FUN_SEND_ISO_READ_SYNC, + CMD_FUN_SEND_ISO_WRITE_ASYNC, + CMD_FUN_SEND_ISO_WRITE_SYNC, + CMD_FUN_SET_CONFIG, + CMD_FUN_SET_CURRENT_FUNCTIONS, + CMD_FUN_SET_INTERFACE, + CMD_ADD_INTERFACE, + CMD_REMOVE_INTERFACE, + CMD_GET_CHARGE_STATE, + CMD_GET_DEVICES, + CMD_HAS_RIGHT, + CMD_READ_DATA_SYNC, + CMD_READ_PARM, + CMD_SET_BAUDRATE, + CMD_SET_ROLE, + CMD_QUERY_PORT, + CMD_STD_CTRL_GET_CONFIGURATION, + CMD_STD_CTRL_GET_DESCRIPTOR_ASYNC, + CMD_STD_CTRL_GET_DESCRIPTOR_CMD, + CMD_STD_CTRL_GET_INTERFACE, + CMD_STD_CTRL_GET_STATUS_CMD, + CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT, + CMD_FUN_REQUEST_QUEUE, + CMD_FUN_REQUEST_WAIT, + CMD_FUN_REQUEST_CANCEL +}; + +struct UsbdDevice { + uint8_t busNum; + uint8_t devAddr; + uint8_t interface; + uint8_t endPoint; +}; +struct UsbdPeripheral { + uint8_t busNum; + uint8_t devAddr; + uint8_t iSerialNumber; +}; +struct UsbdPort { + uint8_t modes; + uint8_t roles; +}; +struct UsbdRequestDataSync { + uint8_t endPoint; + int32_t *requested; + uint8_t *data; + int32_t length; + uint32_t timeout; +}; +struct UsbdRequestDataAsync { + unsigned char endPoint; + unsigned char *buffer; + int length; + int numIsoPackets; + UsbdRequestCallback callback; + void *userData; + unsigned int timeout; +}; +struct UsbdControlRequestSetupData { + uint8_t requestType; + uint8_t requestCmd; + uint16_t value; + uint16_t index; + uint8_t *data; + uint16_t length; + uint32_t timeout; +}; +struct UsbdDescriptorParam { + uint8_t descType; + uint8_t descIndex; + int length; +}; +struct UsbdDescriptorHeader { + uint8_t bLength; + uint8_t bDescriptorType; +} __attribute__ ((packed)); + +struct UsbdDeviceDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize0; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +} __attribute__ ((packed)); + +struct UsbdConfigDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t bMaxPower; +} __attribute__ ((packed)); + +struct UsbdInterfaceDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; +} __attribute__ ((packed)); + +struct UsbdEndpointDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint16_t wMaxPacketSize; + uint8_t bInterval; +} __attribute__ ((packed)); + +struct UsbNotifyServiceInfo { + uint32_t length; + + int32_t devNum; + int32_t busNum; + + int32_t interfaceLength; + uint8_t interfaceNumber[USB_MAX_INTERFACES]; +} __attribute__ ((packed)); + +enum PortRoleType { + DATA_ROLE, + POWER_ROLE, + MODE +}; + +enum PortPowerRole{ + SOURCE, + SINK +}; + +enum PortMode{ + HOST, + DEVICE, + OTG +}; + +struct PortInfo +{ + int32_t portId; + int32_t powerRole; + int32_t dataRole; +}; + +// Keep it same as the inner kit usb_info.h +struct UsbdInfo { + int32_t capacity_; + int32_t voltage_; + int32_t temperature_; + int32_t healthState_; + int32_t pluggedType_; + int32_t pluggedMaxCurrent_; + int32_t pluggedMaxVoltage_; + int32_t chargeState_; + int32_t chargeCounter_; + int8_t present_; + const char *technology_; +}; + +struct UsbdRequest { + +}; + +#endif // USBD_API_H diff --git a/hdi/client/BUILD.gn b/hdi/client/BUILD.gn new file mode 100644 index 00000000..382e5657 --- /dev/null +++ b/hdi/client/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") +import("//build/ohos.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +config("usbd_client_public_config") { + include_dirs = [ + "include", + "${usb_manager_path}/hdi/api/include", + "${usb_manager_path}/interfaces/innerkits/native/include", + ] +} + +ohos_shared_library("usbd_client") { + sources = [ + "src/usbd_client.cpp", + "src/usbd_subscriber.cpp", + ] + + configs = [ "${utils_path}:utils_config" ] + + public_configs = [ ":usbd_client_public_config" ] + + deps = [ + "${hdf_uhdf_path}/hdi:libhdi", + "${hdf_uhdf_path}/host:libhdf_host", + "${hdf_uhdf_path}/osal:libhdf_utils", + "//utils/native/base:utils", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + part_name = "usb_manager_native" +} diff --git a/hdi/client/include/readme.md b/hdi/client/include/readme.md new file mode 100644 index 00000000..1d811a1e --- /dev/null +++ b/hdi/client/include/readme.md @@ -0,0 +1,27 @@ +**USB HDI API 接口** + +| C++接口申明 | C++接口描述 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| int32_t OpenDevice(uint8_t busNum, uint8_t devAddr); | 打开设备,建立连接 | +| int32_t CloseDevice(uint8_t busNum, uint8_t devAddr); | 关闭设备,释放与设备相关的所有系统资源 | +| int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length); | 获取设备描述符 | +| int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, struct DescriptorParams ¶ms); | 获取设备的配置描述符 | +| int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, struct DescriptorParams ¶ms); | 获取设备的字符串描述符 | +| int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex); | 设置设备当前使用的配置,通过配置值进行指定 | +| int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex); | 获取当前生效的配置值 | +| int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface); | 打开接口,并申明独占接口,必须在数据传输前执行 | +| int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface); | 关闭接口,释放接口的占用,在停止数据传输后执行 | +| int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t altIndex); | 设置指定接口的备选设置,用于在具有相同ID但不同备用设置的两个接口之间进行选择 | +| int32_t BulkTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); | 在给定端点上执行批量数据读取, 返回读取的数据和长度,端点方向必须为数据读取 | +| int32_t BulkTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); | 在给定端点上执行批量数据写入, 返回读取的数据和长度,端点方向必须为数据写入 | +| int32_t ControlTransfer(uint8_t busNum, uint8_t devAddr, struct CtrlTransferParamsParams ¶ms); | 对此设备执行端点零的控制事务,传输方向由请求类型决定。 如果requestType& USB_ENDPOINT_DIR_MASK是USB_DIR_OUT ,则传输是写入,如果是USB_DIR_IN ,则传输是读取。 | +| int32_t InterruptTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); | 在给定端点上执行中断数据读取, 返回读取的数据和长度,端点方向必须为数据读取 | +| int32_t InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); | 在给定端点上执行中断数据写入, 返回读取的数据和长度,端点方向必须为数据写入 | +| int32_t IsoTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); | 在给定端点上执行等时数据读取, 返回读取的数据和长度,端点方向必须为数据读取 | +| int32_t IsoTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); | 在给定端点上执行等时数据写入, 返回读取的数据和长度,端点方向必须为数据写入 | +| int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointid, struct RequestParams ¶ms); | 将指定的端点进行异步数据发送或者接收请求,数据传输方向由端点方向决定 | +| int32_t RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, struct RequestParams ¶ms); | 等待RequestQueue异步请求的操作结果 | +| int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t endpoint); | 取消待处理的数据请求 | +| int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole); | 设置端口工作模式(从设备) | +| int32_t GetCurrentFunctions(uint8_t busNum, uint8_t devAddr, int32_t *funcs) | 获取从设备支持的功能列表(按位域表示)(从设备) | +| int32_t SetCurrentFunctions(uint8_t busNum, uint8_t devAddr, int32_t funcs) | 设置从设备支持的功能列表(按位域表示)(从设备) | \ No newline at end of file diff --git a/hdi/client/include/usbd_client.h b/hdi/client/include/usbd_client.h new file mode 100644 index 00000000..106843d2 --- /dev/null +++ b/hdi/client/include/usbd_client.h @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBD_CLIENT_H +#define USBD_CLIENT_H + +#include "usb_info.h" +#include "usbd_subscriber.h" +#include "sp_singleton.h" +#include "usbd_api.h" + +namespace OHOS { +namespace USB { + struct DescriptorParams{ + uint8_t DescId; + uint8_t *buffer; + uint32_t &length; + }; + struct TransferParams{ + uint8_t interfaceid; + uint8_t endpointid; + uint8_t *buffer; + uint32_t &length; + int32_t timeout; + }; + struct CtrlTransferParamsParams{ + int32_t requestType; + int32_t request; + int32_t value; + int32_t index; + uint8_t *buffer; + uint32_t &length; + int32_t timeout; + }; + struct RequestParams{ + void *clientData; + uint32_t &clientLength; + uint8_t *buffer; + uint32_t &length; + }; + +class UsbdClient { +public: + /* usb主设备接口 */ +// static int32_t ConnectDevice(uint8_t busNum, uint8_t devAddr, int32_t &fd); +// static int32_t Close(uint8_t busNum, uint8_t devAddr); +// static int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length); +// static int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length); +// static int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length); +// static int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length); +// static int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex); +// static int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex); +// static int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid); +// static int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid); +// static int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex); +// static int32_t BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); +// static int32_t BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); +// static int32_t ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); +// static int32_t InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); +// static int32_t InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); +// static int32_t IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); +// static int32_t IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); +// static int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length); +// static int32_t RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, void* clientData, uint32_t &clientLength, uint8_t *buffer, uint32_t &length); +// static int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid); + static int32_t OpenDevice(uint8_t busNum, uint8_t devAddr); + static int32_t CloseDevice(uint8_t busNum, uint8_t devAddr); + static int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length); + static int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length); + static int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex); + static int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex); + static int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid); + static int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid); + static int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex); + static int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, struct DescriptorParams ¶ms); + static int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, struct DescriptorParams ¶ms); + static int32_t BulkTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); + static int32_t BulkTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); + static int32_t ControlTransfer(uint8_t busNum, uint8_t devAddr, struct CtrlTransferParamsParams ¶ms); + static int32_t InterruptTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); + static int32_t InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); + static int32_t IsoTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); + static int32_t IsoTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms); + static int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointid, struct RequestParams ¶ms); + static int32_t RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, struct RequestParams ¶ms); + static int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid); + + /* usb从设备接口 */ + static int32_t GetCurrentFunctions(); + static bool SetCurrentFunctions(int32_t funcs); + static int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole); + static int32_t QueryPort(int32_t &portId,int32_t &powerRole,int32_t &dataRole,int32_t &mode); + + + static ErrCode BindUsbdSubscriber(const sptr &subscriber); + static ErrCode UnbindUsbdSubscriber(); + static void PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length); +private: + + static int32_t BuildUsbdRequestDataSyncBuf(const UsbdRequestDataSync &req, MessageParcel &data); + static int32_t BuildUsbdRequestDataAsyncBuf(const UsbdRequestDataAsync &req, MessageParcel &data); + static int32_t BuildUsbdCtrlSetupRequestBuf(const UsbdControlRequestSetupData &req, MessageParcel &data); + static int32_t ParseUsbdDeviceDescriptorBuf(MessageParcel &data, UsbdDeviceDescriptor &req); + static int32_t ParseUsbdConfigDescriptorBuf(MessageParcel &data, UsbdConfigDescriptor &req); + static int32_t ParseUsbdInterfaceDescriptorBuf(MessageParcel &data, UsbdInterfaceDescriptor &req); + static int32_t ParseUsbdEndpointDescriptorBuf(MessageParcel &data, UsbdEndpointDescriptor &req); + static int32_t SetDeviceMessage(MessageParcel &data, uint8_t busNum, uint8_t devAddr); + static int32_t SetBufferMessage(MessageParcel &data, const void *buffer, uint32_t length); + static int32_t GetBufferMessage(MessageParcel &data, void *buffer, uint32_t &length); + //static std::map devicePipeList; + static sptr GetUsbdService(); + static ErrCode DoDispatch(uint32_t cmd, MessageParcel &data, MessageParcel &reply); +}; +} // namespace USB +} // namespace OHOS +#endif // USBD_CLIENT_H diff --git a/hdi/client/include/usbd_subscriber.h b/hdi/client/include/usbd_subscriber.h new file mode 100644 index 00000000..f3fd8865 --- /dev/null +++ b/hdi/client/include/usbd_subscriber.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBD_SUBSCRIBER_H +#define USBD_SUBSCRIBER_H + +#include "usb_info.h" +#include "usbd_api.h" +#include "ipc_object_stub.h" + +namespace OHOS { +namespace USB { + +class UsbdSubscriber : public IPCObjectStub { +public: + explicit UsbdSubscriber() : IPCObjectStub(u"ohos.usb.IUsbManagerSubscriber") {}; + virtual ~UsbdSubscriber() = default; + virtual int32_t DeviceEvent(const UsbInfo &info) = 0; + virtual int32_t PortChangedEvent(int32_t portId, int32_t powerRole, int32_t dataRole) = 0; + //virtual int32_t OnFoundDevice(UsbDevice &dev) = 0; + //virtual int32_t OnFoundDevice(UsbPeripheral &dev) = 0; + //virtual int32_t OnFoundPort(UsbPort &port) = 0; + virtual int32_t Update(const UsbInfo &info) = 0; + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +private: + static FoundDevice & ParserFoundDeviceInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + static int32_t ParserUsbInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option, UsbInfo &info); + static int32_t ParserPortInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option, PortInfo &info); +}; +} // namespace USB +} // namespace OHOS + +#endif // USBD_SUBSCRIBER_H diff --git a/hdi/client/src/usbd_client.cpp b/hdi/client/src/usbd_client.cpp new file mode 100644 index 00000000..3cfe8dbd --- /dev/null +++ b/hdi/client/src/usbd_client.cpp @@ -0,0 +1,718 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usbd_client.h" +#include +#include "iservmgr_hdi.h" +#include "hilog_wrapper.h" +#include "message_parcel.h" +#include "securec.h" +#include "usb_common.h" +#include "usbd_api.h" + +namespace OHOS { +namespace USB { +using OHOS::HDI::ServiceManager::V1_0::IServiceManager; + +namespace { +const std::string USBD_SERVICE = "usbd"; +} + +void UsbdClient::PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length) +{ + std::ostringstream oss; + if(title == NULL || buffer == nullptr || length == 0) { + return; + } + oss.str(""); + oss << title << " << 二进制数据流[" << length << "字节] >> :" ; + for(uint32_t i = 0; i < length; ++i) { + oss << " " << std::hex << (int)buffer[i]; + } + oss << " --> " << buffer << std::endl; + USB_HILOGD(MODULE_USB_SERVICE, "%{public}s", oss.str().c_str()); +} + +sptr UsbdClient::GetUsbdService() { + auto serviceManager = IServiceManager::Get(); + if (serviceManager == nullptr) { + USB_HILOGW(MODULE_USBD, "service manager is nullptr"); + return nullptr; + } + auto UsbdService = serviceManager->GetService(USBD_SERVICE.c_str()); + if (UsbdService == nullptr) { + USB_HILOGW(MODULE_USBD, "Usbd service is nullptr"); + return nullptr; + } + return UsbdService; +} + +ErrCode UsbdClient::BindUsbdSubscriber(const sptr &subscriber) { + USB_HILOGD(MODULE_USBD, "BindUsbdSubscriber enter"); + if (subscriber == nullptr) { + USB_HILOGW(MODULE_USBD, "subscriber is nullptr"); + return ERR_INVALID_VALUE; + } + + MessageParcel data; + MessageParcel reply; + data.WriteRemoteObject(subscriber); + return DoDispatch(CMD_BIND_USB_SUBSCRIBER, data, reply); +} + +ErrCode UsbdClient::UnbindUsbdSubscriber() { + USB_HILOGD(MODULE_USBD, "UnbindUsbdSubscriber enter"); + MessageParcel data; + MessageParcel reply; + return DoDispatch(CMD_UNBIND_USB_SUBSCRIBER, data, reply); +} + +int32_t UsbdClient::OpenDevice(uint8_t busNum, uint8_t devAddr) { + MessageParcel data; + MessageParcel reply; + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d OpenDevice bus:%{public}d dev:%{public}d", __func__, __LINE__, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + ErrCode ret = DoDispatch(CMD_FUN_OPEN_DEVICE, data, reply); + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d OpenDevice ret:%{public}d bus:%{public}d dev:%{public}d", __func__, __LINE__, ret, busNum, devAddr); + if (FAILED(ret)) { + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d CMD_FUN_OPEN_DEVICE failed, return %{public}d", __func__, __LINE__, ret); + return ERR_INVALID_VALUE; + } + + //READ_PARCEL_WITH_RET(reply, Int32, fd, E_READ_PARCEL_ERROR); + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d OpenDevice bus:%{public}d dev:%{public}d ", __func__, __LINE__, busNum, devAddr); + return ERR_OK; +} + +int32_t UsbdClient::GetCurrentFunctions() { + USB_HILOGD(MODULE_USBD, "GetCurrentFunctions enter"); + MessageParcel data; + MessageParcel reply; + ErrCode ret = DoDispatch(CMD_FUN_GET_CURRENT_FUNCTIONS, data, reply); + if (FAILED(ret)) { + USB_HILOGW(MODULE_USBD, "CMD_FUN_GET_CURRENT_FUNCTIONS failed, return INVALID_USB_INT_VALUE"); + return ERR_INVALID_VALUE; + } + int32_t funcs = 0; + READ_PARCEL_WITH_RET(reply, Int32, funcs, E_READ_PARCEL_ERROR); + USB_HILOGD(MODULE_USBD, "GetCurrentFunctions funcs %{public}d", funcs); + return funcs; +} + +bool UsbdClient::SetCurrentFunctions(int32_t funcs) { + USB_HILOGD(MODULE_USBD, "SetCurrentFunctions enter"); + MessageParcel data; + MessageParcel reply; + WRITE_PARCEL_WITH_RET(data, Int32, funcs, E_WRITE_PARCEL_ERROR); + USB_HILOGD(MODULE_USBD, "SetCurrentFunctions funcs %{public}d", funcs); + ErrCode ret = DoDispatch(CMD_FUN_SET_CURRENT_FUNCTIONS, data, reply); + if (FAILED(ret)) { + USB_HILOGW(MODULE_USBD, "CMD_FUN_SET_CURRENT_FUNCTIONS failed"); + return false; + } + int result = 0; + READ_PARCEL_WITH_RET(reply, Int32, result, E_READ_PARCEL_ERROR); + if(result != 0){ + return false; + } + return true; +} + +int32_t UsbdClient::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) { + USB_HILOGD(MODULE_USBD, "UsbdClient::SetPortRole enter"); + MessageParcel data; + MessageParcel reply; + + WRITE_PARCEL_WITH_RET(data, Int32, portId, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, powerRole, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, dataRole, E_WRITE_PARCEL_ERROR); + + ErrCode ret = DoDispatch(CMD_SET_ROLE, data, reply); + int32_t result = -1; + READ_PARCEL_WITH_RET(reply, Int32, result , E_READ_PARCEL_ERROR); + if (FAILED(ret)) { + USB_HILOGW(MODULE_USBD, "CMD_SET_ROLE failed, return INVALID_STRING_VALUE"); + } + return result; +} + +int32_t UsbdClient::QueryPort(int32_t &portId,int32_t &powerRole,int32_t &dataRole, int32_t &mode){ + USB_HILOGD(MODULE_USBD, "UsbdClient::queryPort enter"); + MessageParcel data; + MessageParcel reply; + ErrCode ret = DoDispatch(CMD_QUERY_PORT, data, reply); + READ_PARCEL_WITH_RET(reply, Int32, portId , E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(reply, Int32, powerRole , E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(reply, Int32, dataRole , E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(reply, Int32, mode , E_READ_PARCEL_ERROR); + if (FAILED(ret)) { + USB_HILOGW(MODULE_USBD, "CMD_QUERY_PORT failed, return INVALID_STRING_VALUE"); + } + USB_HILOGE(MODULE_USBD, "portId:%{public}d powerRole:%{public}d dataRole:%{public}d mode:%{public}d ", portId, powerRole, dataRole, mode); + return ret; +} +// int32_t UsbdClient::GetDevicePipe(const UsbdDevice &dev, UsbdPipe &pip) { +// int32_t ret = ERR_INVALID_VALUE; +// auto it = devicePipeList.find(dev); +// if(it != devicePipeList.end()) { +// pip = it->second; +// ret = ERR_OK; +// } +// return ret; +// } +// int32_t UsbdClient::GetPeripheralPipe(const UsbdDevice &dev, UsbdPipe &pip) { +// int32_t ret = ERR_INVALID_VALUE; +// auto it = peripheralPipeList.find(dev); +// if(it != peripheralPipeList.end()) { +// pip = it->second; +// ret = ERR_OK; +// } +// return ret; +// } + + +ErrCode UsbdClient::DoDispatch(uint32_t cmd, MessageParcel &data, MessageParcel &reply) +{ + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d cmd:%{public}d", __func__, __LINE__, cmd); + auto Usbd = GetUsbdService(); + if (Usbd == nullptr) { + USB_HILOGE(MODULE_USBD, "fumang get usbd service failed."); + return ERR_NO_INIT; + } + MessageOption option; + auto ret = Usbd->SendRequest(cmd, data, reply, option); + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d SendRequest end cmd:%{public}d ", __func__, __LINE__, cmd); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USBD, "failed to send request, cmd: %{public}d, ret: %{public}d", cmd, ret); + return ret; + } + USB_HILOGD(MODULE_USBD, "fumang success to dispatch cmd: %{public}d", cmd); + return ERR_OK; +} + +int32_t UsbdClient::BuildUsbdRequestDataSyncBuf(const UsbdRequestDataSync &req, MessageParcel &data) +{ + // 有问题???,不能把指针和回调函数地址传到下层 + WRITE_PARCEL_WITH_RET(data, Uint8, req.endPoint, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int64, (int64_t)(req.requested), E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, req.length, E_WRITE_PARCEL_ERROR); + if (!data.WriteRawData(req.data, req.length)) { + USB_HILOGE(MODULE_COMMON, "%{public}s writeRawdata failed", __func__); + return (E_WRITE_PARCEL_ERROR); + } + WRITE_PARCEL_WITH_RET(data, Uint32, req.timeout, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} +int32_t UsbdClient::BuildUsbdRequestDataAsyncBuf(const UsbdRequestDataAsync &req, MessageParcel &data) +{ + // 有问题???,不能把指针和回调函数地址传到下层 + data.WriteUint8(req.endPoint); + data.WriteInt32(req.length); + data.WriteBuffer(req.buffer, req.length); + data.WriteInt32(req.numIsoPackets); + data.WriteInt64((int64_t)(req.callback)); + + // 有问题??? + data.WriteInt64((int64_t)(req.userData)); + data.WriteUint32(req.timeout); + return ERR_OK; +} + +int32_t UsbdClient::BuildUsbdCtrlSetupRequestBuf(const UsbdControlRequestSetupData &ctrl, MessageParcel &data) +{ + data.WriteUint8(ctrl.requestType); + data.WriteUint8(ctrl.requestCmd); + data.WriteUint16(ctrl.value); + data.WriteUint16(ctrl.index); + data.WriteUint16(ctrl.length); + data.WriteBuffer(ctrl.data, ctrl.length); + data.WriteUint32(ctrl.timeout); + return ERR_OK; +} + +int32_t UsbdClient::ParseUsbdDeviceDescriptorBuf(MessageParcel &data, UsbdDeviceDescriptor &req) +{ + req.bLength = data.ReadUint8(); + req.bDescriptorType = data.ReadUint8(); + req.bcdUSB = data.ReadUint16(); + req.bDeviceClass = data.ReadUint8(); + req.bDeviceSubClass = data.ReadUint8(); + req.bDeviceProtocol = data.ReadUint8(); + req.bMaxPacketSize0 = data.ReadUint8(); + req.idVendor = data.ReadUint16(); + req.idProduct = data.ReadUint16(); + req.bcdDevice = data.ReadUint16(); + req.iManufacturer = data.ReadUint8(); + req.iProduct = data.ReadUint8(); + req.iSerialNumber = data.ReadUint8(); + req.bNumConfigurations = data.ReadUint8(); + return ERR_OK; +} + +int32_t UsbdClient::ParseUsbdConfigDescriptorBuf(MessageParcel &data, UsbdConfigDescriptor &req) +{ + data.ReadUint8(req.bLength); + data.ReadUint8(req.bDescriptorType); + data.ReadUint16(req.wTotalLength); + data.ReadUint8(req.bNumInterfaces); + data.ReadUint8(req.bConfigurationValue); + data.ReadUint8(req.iConfiguration); + data.ReadUint8(req.bmAttributes); + data.ReadUint8(req.bMaxPower); + return ERR_OK; +} + +int32_t UsbdClient::ParseUsbdInterfaceDescriptorBuf(MessageParcel &data, UsbdInterfaceDescriptor &req) +{ + data.ReadUint8(req.bLength); + data.ReadUint8(req.bDescriptorType); + data.ReadUint8(req.bInterfaceNumber); + data.ReadUint8(req.bAlternateSetting); + data.ReadUint8(req.bNumEndpoints); + data.ReadUint8(req.bInterfaceClass); + data.ReadUint8(req.bInterfaceSubClass); + data.ReadUint8(req.bInterfaceProtocol); + data.ReadUint8(req.iInterface); + return ERR_OK; +} + +int32_t UsbdClient::ParseUsbdEndpointDescriptorBuf(MessageParcel &data, UsbdEndpointDescriptor &req) +{ + data.ReadUint8(req.bLength); + data.ReadUint8(req.bDescriptorType); + data.ReadUint8(req.bEndpointAddress); + data.ReadUint8(req.bmAttributes); + data.ReadUint16(req.wMaxPacketSize); + data.ReadUint8(req.bInterval); + return ERR_OK; +} + +int32_t UsbdClient::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *desc, uint32_t &length) +{ + int32_t ret = ERR_OK; + if(desc == NULL || length == 0) { + return ERR_INVALID_VALUE; + } + MessageParcel data; + MessageParcel reply; + UsbdClient::SetDeviceMessage(data, busNum, devAddr); + ret = UsbdClient::DoDispatch(CMD_FUN_GET_CONFIG_DESCRIPTOR, data, reply); + if(ERR_OK != ret) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + uint32_t descLength = 0; + reply.ReadUint32(descLength); + if(descLength > length) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ERR_OVERFLOW; + } + const uint8_t *readData = reply.ReadBuffer(descLength); + memcpy_s(desc, length, readData, descLength); + return ret; +} + +int32_t UsbdClient::GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + UsbdClient::SetDeviceMessage(data, busNum, devAddr); + ret = UsbdClient::DoDispatch(CMD_FUN_GET_DEVICE_DESCRIPTOR, data, reply); + if(ERR_OK != ret) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + + GetBufferMessage(reply, buffer, length); + struct UsbdDeviceDescriptor *devDesc = (struct UsbdDeviceDescriptor*)buffer; + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang bLength=%{public}d, \ + bDescriptorType=%{public}d, \ + bcdUSB=%{public}d, \ + bDeviceClass=%{public}d, \ + bDeviceSubClass=%{public}d, \ + bDeviceProtocol=%{public}d, \ + bMaxPacketSize0=%{public}d, \ + idVendor=%{public}d, \ + idProduct=%{public}d, \ + bcdDevice=%{public}d, \ + iManufacturer=%{public}d, \ + iProduct=%{public}d, \ + iSerialNumber=%{public}d, \ + bNumConfigurations=%{public}d len:%{public}d", + __func__, devDesc->bLength, + devDesc->bDescriptorType, + devDesc->bcdUSB, + devDesc->bDeviceClass, + devDesc->bDeviceSubClass, + devDesc->bDeviceProtocol, + devDesc->bMaxPacketSize0, + devDesc->idVendor, + devDesc->idProduct, + devDesc->bcdDevice, + devDesc->iManufacturer, + devDesc->iProduct, + devDesc->iSerialNumber, + devDesc->bNumConfigurations, + length + ); + + return ret; +} + + +int32_t UsbdClient::GetStringDescriptor(uint8_t busNum, uint8_t devAddr, struct DescriptorParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d cfgId:%{public}d length:%{public}d ", + __func__, __LINE__, params.DescId, params.length); + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.DescId, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_GET_STRING_DESCRIPTOR, data, reply); + if(ERR_OK != ret) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + GetBufferMessage(reply, params.buffer, params.length); + PrintBuffer("UsbdClient::GetStringDescriptor", params.buffer, params.length); + return ret; +} + +int32_t UsbdClient::GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, struct DescriptorParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d cfgId:%{public}d length:%{public}d ", + __func__, __LINE__, params.DescId, params.length); + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.DescId, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_GET_CONFIG_DESCRIPTOR, data, reply); + if(ERR_OK != ret) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + GetBufferMessage(reply, params.buffer, params.length); + PrintBuffer("UsbdClient::GetConfigDescriptor", params.buffer, params.length); + return ret; +} + +int32_t UsbdClient::SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) { + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, configIndex, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_SET_CONFIG, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + return ret; +} + +int32_t UsbdClient::GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) { + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + ret = UsbdClient::DoDispatch(CMD_FUN_GET_CONFIG, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + READ_PARCEL_WITH_RET(data, Uint8, configIndex, E_READ_PARCEL_ERROR); + return ret; +} + +int32_t UsbdClient::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceIndex) { + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interfaceIndex, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_CLAIM_INTERFACE, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + return ret; +} + +int32_t UsbdClient::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceIndex) { + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interfaceIndex, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_RELEASE_INTERFACE, data, reply); + if(ERR_OK != ret) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + return ret; +} + +int32_t UsbdClient::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceIndex, uint8_t altIndex) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interfaceIndex, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, altIndex, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_SET_INTERFACE, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + return ERR_OK; +} + +int32_t UsbdClient::BulkTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, params.endpointid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.timeout, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_SEND_BULK_READ_SYNC, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + + return GetBufferMessage(reply, params.buffer, params.length); +} + +int32_t UsbdClient::BulkTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, params.endpointid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.timeout, E_WRITE_PARCEL_ERROR); + SetBufferMessage(data, params.buffer, params.length); + ret = UsbdClient::DoDispatch(CMD_FUN_SEND_BULK_WRITE_SYNC, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + return ret; +} + +int32_t UsbdClient::ControlTransfer(uint8_t busNum, uint8_t devAddr, struct CtrlTransferParamsParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Int32, params.requestType, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.request, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.value, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.index, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.timeout, E_WRITE_PARCEL_ERROR); + bool bWrite = (params.requestType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT; + if(bWrite) { + SetBufferMessage(data, params.buffer, params.length); + } + ret = UsbdClient::DoDispatch(CMD_FUN_SEND_CTRL_REQUEST_SYNC, data, reply); + if(ret != ERR_OK) { + return ret; + } + if(!bWrite) { + GetBufferMessage(reply, params.buffer, params.length); + PrintBuffer("UsbdClient::ControlTransfer", (const uint8_t*)params.buffer, params.length); + } + return ret; +} + +int32_t UsbdClient::InterruptTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, params.endpointid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.timeout, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_SEND_INTERRUPT_READ_SYNC, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + + return GetBufferMessage(reply, params.buffer, params.length); +} + +int32_t UsbdClient::InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, params.endpointid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.timeout, E_WRITE_PARCEL_ERROR); + SetBufferMessage(data, params.buffer, params.length); + ret = UsbdClient::DoDispatch(CMD_FUN_SEND_INTERRUPT_WRITE_SYNC, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + return ret; +} + +int32_t UsbdClient::IsoTransferRead(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, params.endpointid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.timeout, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_SEND_ISO_READ_SYNC, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + return ret; + } + + return GetBufferMessage(reply, params.buffer, params.length); +} + +int32_t UsbdClient::IsoTransferWrite(uint8_t busNum, uint8_t devAddr, struct TransferParams ¶ms) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, params.interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, params.endpointid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, params.timeout, E_WRITE_PARCEL_ERROR); + SetBufferMessage(data, params.buffer, params.length); + ret = UsbdClient::DoDispatch(CMD_FUN_SEND_ISO_WRITE_SYNC, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + return ret; +} + +int32_t UsbdClient::CloseDevice(uint8_t busNum, uint8_t devAddr) +{ + int32_t ret = ERR_OK; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + ret = UsbdClient::DoDispatch(CMD_FUN_CLOSE_DEVICE, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s failed", __func__); + } + return ret; +} + +int32_t UsbdClient::RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpoint, struct RequestParams ¶ms) +{ + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, ifId, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, endpoint, E_WRITE_PARCEL_ERROR); + SetBufferMessage(data, params.clientData, params.clientLength); + SetBufferMessage(data, params.buffer, params.length); + return UsbdClient::DoDispatch(CMD_FUN_REQUEST_QUEUE, data, reply); +} + +int32_t UsbdClient::RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, struct RequestParams ¶ms) +{ + int32_t ret; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_REQUEST_WAIT, data, reply); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret); + return ret; + } + GetBufferMessage(reply, params.clientData, params.clientLength); + GetBufferMessage(reply, params.buffer, params.length); + //PrintBuffer("RequestWait:clientData", (uint8_t*)params.clientData, params.clientLength); + //PrintBuffer("RequestWait:buffer", params.buffer, params.length); + return ERR_OK; +} + +int32_t UsbdClient::RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) +{ + int32_t ret; + MessageParcel data; + MessageParcel reply; + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, endpointid, E_WRITE_PARCEL_ERROR); + ret = UsbdClient::DoDispatch(CMD_FUN_REQUEST_CANCEL, data, reply); + if(ret != ERR_OK) { + return ret; + } + + return ERR_OK; +} + +int32_t UsbdClient::SetBufferMessage(MessageParcel &data, const void *buffer, uint32_t length) +{ + if(!data.WriteUint32(length)) { + return ERR_INVALID_VALUE; + } + if((length > 0) && !data.WriteBuffer(buffer, length)) { + return ERR_INVALID_VALUE; + } + return ERR_OK; +} + +int32_t UsbdClient::GetBufferMessage(MessageParcel &data, void *buffer, uint32_t &length) +{ + uint32_t dataSize; + if(!data.ReadUint32(dataSize)) { + return ERR_INVALID_VALUE; + } + if(dataSize == 0) { + length = 0; + return ERR_OK; + } + if(length < dataSize) { + USB_HILOGW(MODULE_USBD, "%{public}s failed buffer overflow", __func__); + return ERR_NO_MEMORY; + } + const uint8_t *readData = data.ReadBuffer(dataSize); // +1 避免0大小的空buffer + if(readData == nullptr) { + return ERR_INVALID_VALUE; + } + memcpy_s(buffer, length, readData, dataSize); + USB_HILOGW(MODULE_USBD, "%{public}s Get buffer message. length = %{public}d, dataSize = %{public}d", __func__, length, dataSize); + length = dataSize; + USB_HILOGW(MODULE_USBD, "%{public}s Get buffer message. length = %{public}d", __func__, length); + return ERR_OK; +} + +int32_t UsbdClient::SetDeviceMessage(MessageParcel &data, uint8_t busNum, uint8_t devAddr) +{ + WRITE_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} +} // namespace USB +} // namespace OHOS diff --git a/hdi/client/src/usbd_subscriber.cpp b/hdi/client/src/usbd_subscriber.cpp new file mode 100644 index 00000000..9f4f60bc --- /dev/null +++ b/hdi/client/src/usbd_subscriber.cpp @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usbd_subscriber.h" +#include "usb_common.h" + +namespace OHOS { +namespace USB { + FoundDevice & UsbdSubscriber::ParserFoundDeviceInfo(MessageParcel &data, MessageParcel &reply, + MessageOption &option) + { + static FoundDevice t; + return t; + } +int UsbdSubscriber::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + int32_t ret = ERR_OK; + UsbInfo info; + PortInfo pinfo; + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT entry code:%{public}d", __func__, __LINE__, code); + switch (code) { + case CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT: + USB_HILOGW(MODULE_USBD, "%{public}s CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT entry", __func__); + ParserUsbInfo(data, reply, option, info); + USB_HILOGD(MODULE_USBD, + "%{public}s CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT Read devInfo " + "status=%{public}d,busNum=%{public}d,devAddr=%{public}d", + __func__, info.getDevInfoStatus(), info.getDevInfoBusNum(), info.getDevInfoDevNum()); + ret = DeviceEvent(info); + break; + case CMD_NOTIFY_PORT_CHANGED: + USB_HILOGW(MODULE_USBD, "%{public}s CMD_NOTIFY_PORT_CHANGED entry", __func__); + ParserPortInfo(data, reply, option, pinfo); + ret = PortChangedEvent(pinfo.portId, pinfo.powerRole, pinfo.dataRole); + break; + default: + USB_HILOGW(MODULE_USBD, "no specific cmd for code: %{public}d", code); + ret = ERR_INVALID_OPERATION; + break; + } + return ret; +} + +int32_t UsbdSubscriber::ParserUsbInfo(MessageParcel &data, MessageParcel &reply, + MessageOption &option, UsbInfo &info) +{ + int32_t status; + int32_t busNum; + int32_t devNum; + + if(!data.ReadInt32(status)){ + USB_HILOGW(MODULE_USBD, "Read devInfo status error"); + } + if(!data.ReadInt32(busNum)){ + USB_HILOGW(MODULE_USBD, "Read devInfo busNum error"); + } + if(!data.ReadInt32(devNum)){ + USB_HILOGW(MODULE_USBD, "Read devInfo devNum error"); + } + info.setDevInfoStatus(status); + info.setDevInfoBusNum(busNum); + info.setDevInfoDevNum(devNum); + + USB_HILOGD(MODULE_USBD, "%{public}s Read devInfo status=%{public}d,busNum=%{public}d,devAddr=%{public}d", + __func__, status, busNum, devNum); + return 1; +} + + int32_t UsbdSubscriber::ParserPortInfo(MessageParcel &data, MessageParcel &reply, MessageOption &option, + PortInfo &info) { + int32_t portId; + int32_t powerRole; + int32_t dataRole; + + if (!data.ReadInt32(portId)) { + USB_HILOGW(MODULE_USBD, "Read PortInfo id error"); + return 0; + } + if (!data.ReadInt32(powerRole)) { + USB_HILOGW(MODULE_USBD, "Read PortInfo powerNum error"); + return 0; + } + if (!data.ReadInt32(dataRole)) { + USB_HILOGW(MODULE_USBD, "Read PortInfo dataNum error"); + return 0; + } + info.portId = portId; + info.powerRole = powerRole; + info.dataRole = dataRole; + return 1; +} +} // namespace USB +} // namespace OHOS \ No newline at end of file diff --git a/hdi/service/BUILD.gn b/hdi/service/BUILD.gn new file mode 100644 index 00000000..b41e6901 --- /dev/null +++ b/hdi/service/BUILD.gn @@ -0,0 +1,85 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") +import("//build/ohos.gni") +import("//drivers/adapter/uhdf2/uhdf.gni") + +config("usbd_private_config") { + include_dirs = [ + ] +} + +config("usbd_public_config") { + include_dirs = [ + "include", + "${usb_manager_path}/hdi/api/include", + "//drivers/adapter/uhdf2/include/hdi", + "//drivers/framework/include/utils", + "//drivers/adapter/uhdf2/shared/include", + "//base/notification/ces_standard/cesfwk/kits/native/include", + "//foundation/aafwk/standard/interfaces/innerkits/want/include", + "//drivers/framework/include/core", + "//drivers/peripheral/usb/ddk/common/include", + "//drivers/peripheral/usb/ddk/host/include", + "//drivers/peripheral/usb/interfaces/ddk/common", + "//drivers/peripheral/usb/interfaces/ddk/host", + "//drivers/peripheral/usb/interfaces/ddk/device", + "//drivers/peripheral/usb/gadget/function/include", + "//drivers/framework/core/shared/include", + "//drivers/framework/core/adapter/syscall/include", + "//drivers/framework/model/usb/include", + "//base/startup/init_lite/services/include/param", + "//base/startup/init_lite/services/include", + "//base/startup/init_lite/services/log", + ] +} + +ohos_shared_library("usbd") { + sources = [ + "src/usbd.c", + "src/usbd_dispatcher.c", + #"src/usbd_parser.c", + "src/usbd_publisher.c", + "src/usbd_function.c", + "src/usbd_port.c" + ] + + configs = [ + "${utils_path}:utils_config", + ":usbd_private_config", + ] + + public_configs = [ ":usbd_public_config" ] + + deps = [ + "${hdf_uhdf_path}/host:libhdf_host", + "${hdf_uhdf_path}/ipc:libhdf_ipc_adapter", + "${hdf_uhdf_path}/osal:libhdf_utils", + "//utils/native/base:utils", + "${hdf_uhdf_path}/hdi:libhdi", + "//drivers/peripheral/usb:libusb_ddk", + "//drivers/adapter/uhdf2/config:uhdf_hcs_pkg", + "//drivers/adapter/uhdf2/host:libhdf_host", + "//drivers/peripheral/usb/ddk:libusb_core", + "//base/startup/init_lite/services/param:param_client", + "//third_party/bounds_checking_function:libsec_static", + "//third_party/cJSON:cjson_static", + ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + + part_name = "usb_manager_native" + + subsystem_name = "hdf" +} diff --git a/hdi/service/include/usbd.h b/hdi/service/include/usbd.h new file mode 100644 index 00000000..f4e12bd7 --- /dev/null +++ b/hdi/service/include/usbd.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBD_H +#define USBD_H + +#include "usbd_api.h" +#include "hdf_base.h" +#include "hdf_device_desc.h" +#include "osal_atomic.h" +#include "osal_sem.h" +#include "usb_ddk.h" +#include "usb_session.h" +#include "usb_interface.h" +#include "data_fifo.h" +#include "usbd_publisher.h" + + +#define USB_MAX_INTERFACES 32 +#define DIRECTION_MASK 0x1 +#define USB_CTRL_SET_TIMEOUT 5000 +#define USB_PIPE_DIR_OFFSET 7 + +struct UsbdService; +struct HostDevice { + struct HdfSListNode node; + struct UsbdService *service; + struct DataFifo readFifo; + struct HdfSList requestQueue; + struct OsalMutex requestLock; + uint8_t interfaceIndex[USB_MAX_INTERFACES]; + uint8_t interfaceCnt; + struct UsbInterface *iface[USB_MAX_INTERFACES]; + UsbInterfaceHandle *ctrDevHandle; + UsbInterfaceHandle *devHandle[USB_MAX_INTERFACES]; + struct OsalMutex writeLock; + struct OsalMutex readLock; + struct OsalMutex lock; + struct UsbRequest *ctrlReq; + struct UsbInterface *ctrIface; + struct UsbPipeInfo *ctrPipe; + uint8_t busNum; + uint8_t devAddr; + bool initFlag; +}; + +struct RequestMsg { + struct HdfSListNode node; + struct UsbRequest *request; + void *clientData; + uint32_t clientLength; +}; + +struct UsbControlParams { + uint8_t request; + UsbRequestTargetType target; + UsbControlRequestType reqType; + UsbRequestDirection directon; + uint16_t value; + uint16_t index; + void *data; + uint16_t size; +}; + +struct UsbDescriptorParams { + UsbInterfaceHandle *devHandle; + struct UsbRequest *request; + uint8_t type; + uint8_t index; + void *buf; + uint16_t size; +}; + +struct UsbdService { + struct IDeviceIoService service; + struct HdfDeviceObject *device; + struct UsbdSubscriber *subscriber; + struct UsbSession *session; + //struct HostDevice *port; + struct HdfSList devList; + struct OsalMutex lock; +}; + +struct UsbdSubscriber; +int32_t BindUsbSubscriber(struct UsbdService *service, struct UsbdSubscriber *subscriber); +int32_t UnbindUsbSubscriber(struct UsbdService *service); + +#endif // USBD_H diff --git a/hdi/service/include/usbd_dispatcher.h b/hdi/service/include/usbd_dispatcher.h new file mode 100644 index 00000000..bb5e7afa --- /dev/null +++ b/hdi/service/include/usbd_dispatcher.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBD_DISPATCHER_H +#define USBD_DISPATCHER_H + +#include "core/hdf_device_desc.h" + +int32_t UsbdServiceDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply); +#endif // USBD_DISPATCHER_H diff --git a/hdi/service/include/usbd_function.h b/hdi/service/include/usbd_function.h new file mode 100644 index 00000000..01dd34ef --- /dev/null +++ b/hdi/service/include/usbd_function.h @@ -0,0 +1,7 @@ +#ifndef USBD_FUNCTION_H +#define USBD_FUNCTION_H + +int UsbdSetFunction(int funcs); +int32_t UsbdGetFunction(); + +#endif diff --git a/hdi/service/include/usbd_parser.h b/hdi/service/include/usbd_parser.h new file mode 100644 index 00000000..880b0097 --- /dev/null +++ b/hdi/service/include/usbd_parser.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBD_PARSER_H +#define USBD_PARSER_H + +#include "usbd_api.h" + +int32_t InitUsbdSysfs(void); +int32_t ParseCapacity(int32_t *capacity); +int32_t ParseVoltage(int32_t *voltage); +int32_t ParseTemperature(int32_t *temperature); +int32_t ParseHealthState(int32_t *healthState); +int32_t ParsePluggedType(int32_t *pluggedType); +int32_t ParseChargeState(int32_t *chargeState); +int32_t ParsePresent(int8_t *present); +int32_t ParseTechnology(char *technology, size_t size); +void ParseUeventToUsbdInfo(const char *msg, struct UsbdInfo *info); +int32_t SwitchRole(int32_t portId,int32_t powerRole,int32_t dataRole); +int changeRole(const char *portName, enum PortRoleType type, int32_t role); +int32_t readFile(const char *filename, char contents); +char *appendRoleNodeHelper(const char *portName, enum PortRoleType type); +char *convertRoletoString(enum PortRoleType type, int32_t role); + +#endif // USBD_PARSER_H diff --git a/hdi/service/include/usbd_port.h b/hdi/service/include/usbd_port.h new file mode 100644 index 00000000..76db22c6 --- /dev/null +++ b/hdi/service/include/usbd_port.h @@ -0,0 +1,9 @@ +#ifndef USBD_PORT_H +#define USBD_PORT_H + +#include "usbd.h" + +int SetPort(int portId , int powerRole, int dataRole, struct UsbdService *service); +int QueryPort(int *portId , int *powerRole, int *dataRole, int *mode, struct UsbdService *service); + +#endif diff --git a/hdi/service/include/usbd_publisher.h b/hdi/service/include/usbd_publisher.h new file mode 100644 index 00000000..949b3cc9 --- /dev/null +++ b/hdi/service/include/usbd_publisher.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBD_PUBLISHER_H +#define USBD_PUBLISHER_H + +#include "usbd_api.h" +#include "usbd.h" + + +struct UsbdSubscriber { + struct HdfRemoteService *remoteService; +}; + +void NotifyUsbPortSubscriber(const struct UsbdSubscriber *subscriber, int32_t portId, int32_t powerRole, int32_t dataRole); +void NotifySubscriberDevice(const struct UsbdSubscriber *subscriber, enum UsbdDeviceAction act, int32_t busNum, int32_t devNum); + + +#endif // USBD_PUBLISHER_H diff --git a/hdi/service/src/usbd.c b/hdi/service/src/usbd.c new file mode 100644 index 00000000..f66ea76e --- /dev/null +++ b/hdi/service/src/usbd.c @@ -0,0 +1,394 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usbd.h" +#include +#include "osal_mem.h" +#include "osal_time.h" +#include "securec.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "hdf_usb_pnp_manage.h" +#include "usbd_dispatcher.h" +#include "usbd_function.h" +#include "usbd_parser.h" +#include "usbd_publisher.h" +#include "hdf_io_service_if.h" +#include "hdf_usb_pnp_manage.h" +#include "devmgr_service_if.h" +#include "usb_ddk_pnp_loader.h" +#include "hdf_syscall_adapter.h" + +int32_t HdfDeviceRegisterEventListener(struct HdfIoService *target, struct HdfDevEventlistener *listener); + + +#define USB_CTRL_REQ_SIZE 64 +#define HDF_LOG_TAG usbd +#define ERR_INVALID_FD (-1) +#define ERR_OPERATION_FAILED (-1) +#define UEVENT_BUFF_SIZE (64 * 1024) +#define UEVENT_RESERVED_SIZE (2) +#define UEVENT_MSG_LEN (2 * 1024) + + +static int32_t UsbdDriverBind(struct HdfDeviceObject *device); +static int32_t UsbdDriverInit(struct HdfDeviceObject *device); +static void UsbdDriverRelease(struct HdfDeviceObject *device); +int32_t UsbdRealseDevices(struct UsbdService *service); +int32_t HostDeviceCreate(struct HostDevice **port); + +#if 0 +static int32_t UsbdDeviceAlloc(struct UsbdService *dev); +static void UsbdDevicelFree(struct UsbdService *dev); + +static int32_t UsbdDeviceAlloc(struct UsbdService *dev) +{ + struct HostDevice *port = NULL; + + if (dev == NULL) { + HDF_LOGE("%{public}s: dev null pointer", __func__); + return HDF_FAILURE; + } + + port = (struct HostDevice *)OsalMemCalloc(sizeof(*port)); + if (port == NULL) { + HDF_LOGE("%{public}s: Alloc usb host device failed", __func__); + return HDF_FAILURE; + } + + if (OsalMutexInit(&port->lock) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: init lock fail!", __func__); + return HDF_FAILURE; + } + if (OsalMutexInit(&port->requestLock) != HDF_SUCCESS) { + HDF_LOGE("%{public}s: init lock fail!", __func__); + return HDF_FAILURE; + } + HdfSListInit(&port->requestQueue); + OsalSemInit(&port->requestSem, 0); + dev->port = port; + port->dev = dev; + + return HDF_SUCCESS; +} + +static void UsbdDevicelFree(struct UsbdService *dev) +{ + struct HostDevice *port = dev->port; + + if (port == NULL) { + HDF_LOGE("%{public}s: port is null", __func__); + return; + } + OsalMemFree(port); +} +#endif + + + +static int UsbdEventHandle(struct UsbdService *inst); + +/* HdfDriverEntry implementations */ +static int32_t UsbdDriverBind(struct HdfDeviceObject *device) +{ + struct UsbdService *dev = NULL; + struct UsbPnpNotifyServiceInfo *info = NULL; + int32_t ret; + HDF_LOGE("%{public}s:%{public}d fumang entry", __func__, __LINE__); + if (device == NULL) { + HDF_LOGE("%{public}s:%{public}d device is null", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + dev = (struct UsbdService *)OsalMemCalloc(sizeof(struct UsbdService)); + if (dev == NULL) { + HDF_LOGE("%{public}s: Alloc dev device failed", __func__); + return HDF_FAILURE; + } + memset(dev, 0, sizeof(struct UsbdService)); + if (OsalMutexInit(&dev->lock) != HDF_SUCCESS) { + HDF_LOGE(" init lock fail!"); + return HDF_FAILURE; + } + info = (struct UsbPnpNotifyServiceInfo *)device->priv; + if (info) { + struct HostDevice *port = NULL; + ret = HostDeviceCreate(&port); + if(ret == HDF_SUCCESS){ + port->busNum = info->busNum; + port->devAddr = info->busNum; + port->service = dev; + OsalMutexLock(&dev->lock); + HdfSListAdd(&dev->devList, &port->node); + OsalMutexUnlock(&dev->lock); + } + } + HDF_LOGE("%{public}s: fumang exit", __func__); + device->service = &(dev->service); + device->service->Dispatch = UsbdServiceDispatch; + dev->device = device; + UsbdEventHandle(dev); + return HDF_SUCCESS; +} + +static int32_t UsbdDriverInit(struct HdfDeviceObject *device) +{ + int32_t ret = HDF_SUCCESS; + HDF_LOGE("%{public}s:%{public}d fumang exit", __func__, __LINE__); + if (device == NULL) { + HDF_LOGE("%{public}s:%{public}d device is null", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + + HDF_LOGD("%{public}s:%{public}d init ok!", __func__, __LINE__); + return ret; +} + +static void UsbdDriverRelease(struct HdfDeviceObject *device) +{ + struct UsbdService *dev = NULL; + HDF_LOGE("%{public}s:%{public}d fumang exit", __func__, __LINE__); + if (device == NULL) { + HDF_LOGE("%{public}s: device is NULL", __func__); + return; + } + dev = (struct UsbdService *)device->service; + UsbdRealseDevices(dev); + HDF_LOGD("%{public}s:%{public}d exit", __func__, __LINE__); +} + +struct HdfDriverEntry g_usbdDriverEntry = { + .moduleVersion = 1, + .moduleName = "usbd", + .Bind = UsbdDriverBind, + .Init = UsbdDriverInit, + .Release = UsbdDriverRelease, +}; +HDF_INIT(g_usbdDriverEntry); + + +static int UsbdAddDevicesOnStart(struct UsbdService *service); + +int32_t BindUsbSubscriber(struct UsbdService *service, struct UsbdSubscriber *subscriber) +{ + HDF_LOGE("%{public}s: fumang entry", __func__); + HDF_LOGE("%{public}s:%{public}d fumang entry service:%{public}p subscriber:%{public}p", __func__, __LINE__, service, subscriber); + if (service == NULL) { + HDF_LOGW("%{public}s fumang service is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + service->subscriber = subscriber; + UsbdAddDevicesOnStart(service); + return HDF_SUCCESS; +} + +int32_t UnbindUsbSubscriber(struct UsbdService *service) +{ + HDF_LOGE("%{public}s:%{public}d fumang entry", __func__, __LINE__); + if (service == NULL) { + HDF_LOGW("%{public}s service is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + if (service->subscriber != NULL) { + HDF_LOGE("%{public}s:%{public}d fumang entry", __func__, __LINE__); + OsalMemFree(service->subscriber); + service->subscriber = NULL; + } + return HDF_SUCCESS; +} + + + +static int UsbdPnpLoaderEventReceived(void *priv, uint32_t id, struct HdfSBuf *data) +{ + int ret = HDF_SUCCESS; + bool flag = false; + uint32_t infoSize; + struct UsbPnpNotifyMatchInfoTable *infoTable = NULL; + struct UsbdService *super = (struct UsbdService *)priv; + + HDF_LOGI("%{public}s:%{public}d id:%{public}d service:%{public}s subscriber:%{public}s ", __func__, __LINE__, id, + super ? "OK":"NULL", super ? (super->subscriber ? "OK":"NULL") : "NULL"); + if(!super){ + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGI("%{public}s:%{public}d id:%{public}d service:%{public}p subscriber:%{public}p ", __func__, __LINE__, id, + super, super->subscriber); + if(USB_PNP_DRIVER_GADGET_ADD == id){ + NotifySubscriberDevice(super->subscriber, ACT_UPDEVICE, 0, 0); + return HDF_SUCCESS; + }else if(USB_PNP_DRIVER_GADGET_REMOVE == id){ + NotifySubscriberDevice(super->subscriber, ACT_DOWNDEVICE, 0, 0); + return HDF_SUCCESS; + } + flag = HdfSbufReadBuffer(data, (const void **)(&infoTable), &infoSize); + if ((flag == false) || (infoTable == NULL)) { + ret = HDF_ERR_INVALID_PARAM; + HDF_LOGE("%{public}s: fail to read infoTable in event data, flag=%{public}d, infoTable=%{public}p", \ + __func__, flag, infoTable); + return ret; + } + + HDF_LOGI("%{public}s:%{public}d id=%{public}d infoSize=%{public}d, usbDevAddr=0x%{public}x, devNum=%{public}d, \ + busNum=%{public}d, class:%{public}d infoTable=0x%{public}x-0x%{public}x success", + __func__, __LINE__, id, infoSize, infoTable->usbDevAddr, infoTable->devNum, + infoTable->busNum, infoTable->deviceInfo.deviceClass, infoTable->deviceInfo.vendorId, infoTable->deviceInfo.productId); + + if(0x09 == infoTable->deviceInfo.deviceClass){ + HDF_LOGI("%{public}s:%{public}d hub device ret:%{public}d", __func__, __LINE__, ret); + return ret; + } + switch (id) { + //case USB_PNP_NOTIFY_ADD_INTERFACE: + case USB_PNP_NOTIFY_ADD_DEVICE: + //case USB_PNP_NOTIFY_REPORT_INTERFACE: + ret = HDF_SUCCESS; + if(0x09 != infoTable->deviceInfo.deviceClass) + NotifySubscriberDevice(super->subscriber, ACT_DEVUP, infoTable->busNum, infoTable->devNum); + //ret = UsbDdkPnpLoaderDevice(infoTable, super, id); + break; + //case USB_PNP_NOTIFY_REMOVE_INTERFACE: + case USB_PNP_NOTIFY_REMOVE_DEVICE: + ret = HDF_SUCCESS; + if(0x09 != infoTable->deviceInfo.deviceClass) + NotifySubscriberDevice(super->subscriber, ACT_DEVDOWN, infoTable->busNum, infoTable->devNum); + //ret = UsbDdkPnpLoaderRemoveDevice(super, removeInfo, id); + break; + default: + //ret = HDF_ERR_INVALID_PARAM; + ret = HDF_SUCCESS; + break; + } + + HDF_LOGI("%{public}s:%{public}d ret=%{public}d DONE", __func__, __LINE__, ret); + + return ret; +} + +static int UsbdEventHandle(struct UsbdService *inst) +{ + int status; + struct HdfIoService *usbPnpServ = HdfIoServiceBind(USB_PNP_NOTIFY_SERVICE_NAME); + static struct HdfDevEventlistener usbPnpListener = { + .callBack = UsbdPnpLoaderEventReceived, + }; + usbPnpListener.priv = (void *)(inst); + + if (usbPnpServ == NULL) { + HDF_LOGE("%{public}s: HdfIoServiceBind faile.", __func__); + return HDF_ERR_INVALID_OBJECT; + } + + status = HdfDeviceRegisterEventListener(usbPnpServ, &usbPnpListener); + if (status != HDF_SUCCESS) { + HDF_LOGE("HdfDeviceRegisterEventListener faile status=%{public}d", status); + return status; + } + + return HDF_SUCCESS; + //return UsbDdkPnpLoaderEventHandle(); +} + +static int UsbdAddDevicesOnStart(struct UsbdService *service) +{ + int32_t ret = HDF_SUCCESS; + struct HdfIoService *usbPnpServ = HdfIoServiceBind(USB_PNP_NOTIFY_SERVICE_NAME); + struct HdfSBuf *data; + struct HdfSBuf *reply; + int32_t count = 0, size = 0; + + if (service == NULL) { + HDF_LOGE("%{public}s:%{public}d service is NULL.", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + + if (usbPnpServ == NULL) { + HDF_LOGE("%{public}s: HdfIoServiceBind faile serv:%{public}s.", __func__, USB_PNP_NOTIFY_SERVICE_NAME); + return HDF_ERR_INVALID_OBJECT; + } + + data = HdfSBufObtainDefaultSize(); + if (data == NULL) { + ret = HDF_DEV_ERR_NO_MEMORY; + HDF_LOGE("%{public}s: fail to obtain sbuf data", __func__); + return ret; + } + reply = HdfSBufObtainDefaultSize(); + if (reply == NULL) { + ret = HDF_DEV_ERR_NO_MEMORY; + HDF_LOGE("%{public}s: fail to obtain sbuf reply", __func__); + goto out; + } + + ret = usbPnpServ->dispatcher->Dispatch(&usbPnpServ->object, USB_PNP_DRIVER_GETDEVICES, data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: fail to send serivice call, ret=%{public}d", __func__, ret); + goto out; + } + + while(1){ + int32_t busNum; + int32_t devNum; + uint8_t devClass; + uint8_t subClass; + uint8_t protocol; + uint8_t status; + if (!HdfSbufReadInt32(reply, &busNum)) { + //ret = HDF_ERR_INVALID_OBJECT; + HDF_LOGE("%{public}s: fail to get service call reply", __func__); + break; + } + if (!HdfSbufReadInt32(reply, &devNum)) { + //ret = HDF_ERR_INVALID_OBJECT; + HDF_LOGE("%{public}s: fail to get service call reply", __func__); + break; + } + if (!HdfSbufReadUint8(reply, &devClass)) { + ret = HDF_ERR_INVALID_OBJECT; + HDF_LOGE("%{public}s:%{public}d fail to get service call reply", __func__, __LINE__); + break; + } + if (!HdfSbufReadUint8(reply, &subClass)) { + ret = HDF_ERR_INVALID_OBJECT; + HDF_LOGE("%{public}s:%{public}d fail to get service call reply", __func__, __LINE__); + break; + } + if (!HdfSbufReadUint8(reply, &protocol)) { + ret = HDF_ERR_INVALID_OBJECT; + HDF_LOGE("%{public}s:%{public}d fail to get service call reply", __func__, __LINE__); + break; + } + if (!HdfSbufReadUint8(reply, &status)) { + ret = HDF_ERR_INVALID_OBJECT; + HDF_LOGE("%{public}s:%{public}d fail to get service call reply", __func__, __LINE__); + break; + } + HDF_LOGI("%{public}s:%{public}d OnStart get device[%{public}d]:%{public}d:%{public}d status:%{pubic}d class:%{public}d subClass:%{public}d protocol:%{pubic}d", + __func__, __LINE__, count, busNum, devNum, status, devClass, subClass, protocol); + if(devClass != 0x09){ + //if( (devClass != 0x09) || (devNum != 1) ){ + NotifySubscriberDevice(service->subscriber, ACT_DEVUP, busNum, devNum); + ++size; + } + ++count; + } + + HDF_LOGI("%{public}s:%{public}d onStart add devices:%{public}d size:%{public}d success", __func__, __LINE__, count, size); + +out: + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + HdfIoServiceRecycle(usbPnpServ); + return ret; +} diff --git a/hdi/service/src/usbd_dispatcher.c b/hdi/service/src/usbd_dispatcher.c new file mode 100644 index 00000000..4b974cfa --- /dev/null +++ b/hdi/service/src/usbd_dispatcher.c @@ -0,0 +1,2020 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usbd_dispatcher.h" +#include +#include +#include "osal_mem.h" +#include "osal_sem.h" +#include "osal_time.h" +#include "securec.h" +#include "hdf_base.h" +#include "hdf_log.h" +#include "hdf_usb_pnp_manage.h" +#include "usbd.h" +#include "usbd_publisher.h" +#include "usbd_parser.h" +#include "usbd_function.h" +#include "usbd_port.h" +#include "usb_interface_pool.h" + +#define HDF_LOG_TAG usbd_dispatcher + +#define MAX_BUFF_SIZE 16384 +#define MAX_CONTROL_BUFF_SIZE 1024 +#define READ_BUF_SIZE 8192 + +//#define MAX_BUFF_SIZE 256 +//#define MAX_CONTROL_BUFF_SIZE 256 +//#define READ_BUF_SIZE 256 +//HDF_SBUF_DEFAULT_SIZE + +#define USB_CTRL_SET_TIMEOUT 5000 +#define USB_PIPE_DIR_OFFSET 7 +#define CHARFORMAT 8 +#define USB_REUQEST_SLEEP_TIME 100 +#define USB_MAX_DESCRIPTOR_SIZE 256 + +#define OPEN_SLEPP_TIME 1000 +#define SUBMIT_SLEEP_TIME 500 + + +static int32_t DispatchBindUsbSubscriber(struct UsbdService *service, struct HdfSBuf *data); +static int32_t DispatchUnbindUsbSubscriber(struct UsbdService *service); +static int32_t GetPipe(const struct HostDevice *dev, uint8_t interfaceId, uint8_t pipeId, struct UsbPipeInfo *pipe); +static int32_t UsbControlTransfer(struct HostDevice *dev, int32_t requestCmd, int32_t cmdType, int32_t target, int32_t direction, int32_t value, \ + int32_t index, int32_t timeout, uint8_t *buffer, uint32_t *length); + +int32_t HostDeviceCreate(struct HostDevice **port); +int32_t UsbdRealseDevices(struct UsbdService *service); +static struct HostDevice * FindDevFromService(struct UsbdService *service, uint8_t busNum, uint8_t devAddr); +static int32_t RemoveDevFromService(struct UsbdService *service, struct HostDevice *port); + + + +///////////////////////////////////////////////////////common func ///////////////////////////////////////// +void PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length) +{ + if(title == NULL || buffer == NULL || length == 0) { + return; + } + uint32_t logLength = strlen(title) + length * 3 + 50; + char *logBuffer = (char *)OsalMemAlloc(logLength); + if(logBuffer == NULL) { + return; + } + memset(logBuffer, 0, logLength); + sprintf_s(logBuffer, logLength, " %s << 二进制数据流,%u字节 >> :", title, length); + uint32_t pos = strlen(logBuffer); + for(uint32_t i = 0; i < length; ++i) { + sprintf_s(logBuffer + pos, logLength - pos, " %02x", buffer[i]); + pos += 3; + } + sprintf_s(logBuffer + pos, logLength - pos, " --> %s \n", buffer); + HDF_LOGE("%{public}s", logBuffer); + OsalMemFree(logBuffer); +} + +static int32_t ParseDeviceBuf(struct HdfSBuf *data, uint8_t *busNum, uint8_t *devAddr) +{ + if(data == NULL || busNum == NULL || devAddr == NULL) { + return HDF_ERR_INVALID_PARAM; + } + HdfSbufReadUint8(data, busNum); + HdfSbufReadUint8(data, devAddr); + return HDF_SUCCESS; +} + +static int32_t UsbdAllocFifo(struct DataFifo *fifo, uint32_t size) +{ + if (!DataFifoIsInitialized(fifo)) { + void *data = OsalMemAlloc(size); + if (data == NULL) { + HDF_LOGE("%{public}s:%{public}d allocate failed", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + DataFifoInit(fifo, size, data); + } + return HDF_SUCCESS; +} + +static void UsbdFreeFifo(struct DataFifo *fifo) +{ + if (fifo == NULL) { + HDF_LOGE("%{public}s:%{public}d fifo is NULL", __func__, __LINE__); + return; + } + + if (fifo->data != NULL) { + OsalMemFree(fifo->data); + fifo->data = NULL; + } + + DataFifoInit(fifo, 0, NULL); +} + +static struct RequestMsg* UsbdAllocRequestMsg(UsbInterfaceHandle *interfaceHandle, int32_t isoPackets, int32_t length) +{ + struct RequestMsg *reqMsg = NULL; + reqMsg = (struct RequestMsg*)OsalMemAlloc(sizeof(struct RequestMsg)); + if(reqMsg == NULL) { + HDF_LOGE("HDF_ERR_MALLOC_FAIL faild\n"); + return NULL; + } + memset(reqMsg, 0, sizeof(*reqMsg)); + reqMsg->request = UsbAllocRequest(interfaceHandle, 0, length); + if (reqMsg->request == NULL) { + HDF_LOGE("HDF_ERR_MALLOC_FAIL faild\n"); + OsalMemFree(reqMsg); + return NULL; + } + reqMsg->clientData = NULL; + reqMsg->clientLength = 0; + return reqMsg; +} + +static int32_t UsbdFreeRequestMsg(struct RequestMsg *reqMsg) +{ + if(reqMsg == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + if(reqMsg->request != NULL) { + UsbFreeRequest(reqMsg->request); + } + reqMsg->clientData = NULL; + reqMsg->clientLength = 0; + OsalMemFree(reqMsg); + reqMsg = NULL; + + return HDF_SUCCESS; +} + +static void UsbdReadCallback(struct UsbRequest *req) +{ + uint8_t *data = NULL; + uint32_t dataSize = 0, readSize = 0; + //struct UsbIfRequest *reqObj = (struct UsbIfRequest *)req; + HDF_LOGE("%{public}s:%{pulib}d entry", __func__, __LINE__); + if (req == NULL) { + HDF_LOGE("%{public}s:%{pulib}d req is NULL!", __func__, __LINE__); + return; + } + int status = req->compInfo.status; + dataSize = req->compInfo.actualLength; + struct HostDevice *dev = (struct HostDevice *)req->compInfo.userData; + if (dev == NULL) { + HDF_LOGE("%{public}s:%{pulib}d dev is NULL!", __func__, __LINE__); + goto exit; + //return ; + } + + switch (status) { + case 0: + HDF_LOGD("Bulk status: %{public}d+size:%{public}u\n", status, dataSize); + if (dataSize > 0) { + data = req->compInfo.buffer; + OsalMutexLock(&dev->readLock); + if (DataFifoIsFull(&dev->readFifo)) { + HDF_LOGD("%{public}s:%{public}d", __func__, __LINE__); + DataFifoSkip(&dev->readFifo, dataSize); + } + readSize = DataFifoWrite(&dev->readFifo, data, dataSize); + if (readSize != dataSize) { + HDF_LOGW("%{public}s: write %{public}u less than expected %{public}zu", + __func__, dataSize, readSize); + } + + OsalMutexUnlock(&dev->readLock); + } + break; + default: + HDF_LOGE("%{public}s:%{public}d status=%{public}d", __func__, __LINE__, status); + break; + } + +exit: + return ; + //OsalSemPost(&reqObj->hostRequest->sem); + +// //OsalSemPost(&dev->requestSem); +// // retval = UsbSubmitRequestAsync(req); +// // if (retval && retval != -EPERM) { +// // HDF_LOGE("%{public}s - usb_submit_urb failed: %{public}d\n", __func__, retval); +// // } +} + +static void UsbdWriteCallback(struct UsbRequest *req) +{ + HDF_LOGE("%{public}s:%{pulib}d entry", __func__, __LINE__); + if (req == NULL) { + HDF_LOGE("%{public}s:%{pulib}d req is NULL!", __func__, __LINE__); + return ; + } + struct HostDevice *dev = (struct HostDevice *)req->compInfo.userData; + if ((dev == NULL) || (dev == NULL)) { + HDF_LOGE("%{public}s:%{pulib}d dev or dev->port is NULL!", __func__, __LINE__); + return ; + } + int status = req->compInfo.status; + HDF_LOGE("%{public}s:%{pulib}d statue is %d!", __func__, __LINE__, status); + switch (status) { + case 0: + break; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + break; + default: + break; + } + + //struct UsbIfRequest *reqObj = (struct UsbIfRequest *)req; + //OsalSemPost(&reqObj->hostRequest->sem); + return; +} + +static UsbInterfaceHandle *InterfaceIdToHandle(const struct HostDevice *dev, uint8_t id) +{ + UsbInterfaceHandle *devHandle = NULL; + + if (id == 0xFF) { + devHandle = dev->ctrDevHandle; + } else if(id < USB_MAX_INTERFACES) { + // for (uint8_t i = 0; i < dev->interfaceCnt; ++i) { + // if (dev->iface[i]->info.interfaceIndex == id) { + // devHandle = dev->devHandle[i]; + // break; + // } + // } + return dev->devHandle[id]; + } + return devHandle; +} + +static int32_t UsbControlSetUp(struct UsbControlParams *controlParams, struct UsbControlRequest *controlReq) +{ + if(controlParams == NULL || controlReq == NULL) { + return HDF_ERR_INVALID_PARAM; + } + + controlReq->target = controlParams->target; + controlReq->reqType = controlParams->reqType; + controlReq->directon = controlParams->directon; + controlReq->request = controlParams->request; + controlReq->value = CpuToLe16(controlParams->value); + controlReq->index = CpuToLe16(controlParams->index); + controlReq->buffer = controlParams->data; + controlReq->length = CpuToLe16(controlParams->size); + return HDF_SUCCESS; +} + +static struct UsbInterface *GetUsbInterfaceById(const struct HostDevice *dev, uint8_t interfaceIndex) +{ + struct UsbInterface *tmpIf = NULL; + if(dev->service == NULL){ + HDF_LOGE("%{public}s:%{public}d idx:%{public}d service is null", __func__, __LINE__, interfaceIndex); + return NULL; + } + HDF_LOGE("%{public}s:%{public}d UsbClaimInterface start session:%{public}s bus:%{public}d dev:%{public}d idex:%{public}d", __func__, __LINE__, dev->service->session?"OK":"NULL", dev->busNum, dev->devAddr, interfaceIndex); + tmpIf = (struct UsbInterface *)UsbClaimInterface(dev->service->session, dev->busNum, dev->devAddr, interfaceIndex); + HDF_LOGE("%{public}s:%{public}d UsbClaimInterface end session:%{public}s bus:%{public}d dev:%{public}d idex:%{public}d", __func__, __LINE__, dev->service->session?"OK":"NULL", dev->busNum, dev->devAddr, interfaceIndex); + if(tmpIf == NULL) { + HDF_LOGE("%{public}s:%{public}d failed busNum=%{public}d, devAddr=%{public}d, interface=%{public}d", + __func__, __LINE__, dev->busNum, dev->devAddr, interfaceIndex); + } + return tmpIf; +} + + +static int32_t GetInterfacePipe(const struct HostDevice *dev, struct UsbInterface *interface, uint8_t pipeAddr, struct UsbPipeInfo *pipe) +{ + int32_t ret = HDF_FAILURE; + struct UsbInterfaceInfo *info = NULL; + UsbInterfaceHandle *interfaceHandle = NULL; + struct UsbPipeInfo pipeTmp; + memset(&pipeTmp, 0, sizeof(pipeTmp)); + if (dev == NULL || interface == NULL || pipe == NULL) { + HDF_LOGE("%{public}s:%{public}d invalid parmas", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + info = &interface->info; + if (info == NULL) { + HDF_LOGE("%{public}s:%{public}d invalid interface", __func__, __LINE__); + return HDF_FAILURE; + } + + interfaceHandle = InterfaceIdToHandle(dev, info->interfaceIndex); + if (interfaceHandle == NULL) { + HDF_LOGE("%{public}s:%{public}d invalid interface handle", __func__, __LINE__); + return HDF_FAILURE; + } + + for(uint8_t i = 1; i <= info->pipeNum; ++i) { + ret = UsbGetPipeInfo(interfaceHandle, info->curAltSetting, i, &pipeTmp); + HDF_LOGE("%{public}s:%{public}d UsbGetPipeInfo [%{public}d/%{public}d] ret:%{public}d Addr:0x%{public}02x pipeAddr:0x%{public}02x \ + id:%{public}d type:%{public}d direction:%{public}d ifcId:%{public}d", + __func__, __LINE__, i, info->pipeNum, ret, pipeAddr, pipeTmp.pipeAddress, pipeTmp.pipeId, pipeTmp.pipeType, pipeTmp.pipeDirection, + pipeTmp.interfaceId); + if((ret == HDF_SUCCESS) && ((pipeTmp.pipeAddress | (uint8_t)pipeTmp.pipeDirection) == pipeAddr)) { + if(pipe) *pipe = pipeTmp; + return HDF_SUCCESS; + } + } + return HDF_FAILURE; +} + +static int32_t GetPipe(const struct HostDevice *dev, uint8_t interfaceId, uint8_t pipeId, struct UsbPipeInfo *pipe) +{ + int32_t ret = HDF_SUCCESS; + struct UsbInterface *interface = NULL; + + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d invalid parmas", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + if(interfaceId > USB_MAX_INTERFACES) { + HDF_LOGE("%{public}s:%{public}d invalid parmas", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + interface = dev->iface[interfaceId]; + if(interface == NULL) { + HDF_LOGE("%{public}s:%{public}d invalid interface", __func__, __LINE__); + return HDF_FAILURE; + } + + ret = GetInterfacePipe(dev, interface, pipeId, pipe); + return ret; +} + +static void UsbdFreeCtrlPipe(struct HostDevice *dev) +{ + if (dev == NULL) { + return; + } + if (dev->ctrPipe) { + OsalMemFree(dev->ctrPipe); + dev->ctrPipe = NULL; + } +} + +static int32_t UsbdGetCtrlPipe(struct HostDevice *dev) +{ + int32_t ret; + struct UsbPipeInfo *pipe = NULL; + pipe = (struct UsbPipeInfo *)OsalMemAlloc(sizeof(struct UsbPipeInfo)); + memset(pipe, 0, sizeof(struct UsbPipeInfo)); + if(pipe == NULL) { + HDF_LOGE("%{public}s:%{public}d OsalMemAlloc failed", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + + HDF_LOGE("%{public}s: constrol interface info : %{public}d, %{public}d, %{public}d, \ + %{public}d, %{public}d, %{public}d, %{public}d,", __func__, dev->ctrIface->info.interfaceIndex, \ + dev->ctrIface->info.altSettings, \ + dev->ctrIface->info.curAltSetting, \ + dev->ctrIface->info.pipeNum, \ + dev->ctrIface->info.interfaceClass, \ + dev->ctrIface->info.interfaceSubClass, \ + dev->ctrIface->info.interfaceProtocol + ); + ret = UsbGetPipeInfo(dev->ctrDevHandle, dev->ctrIface->info.curAltSetting, 0, pipe); + if(ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d get pipe failed ret = %{public}d", __func__, __LINE__, ret); + OsalMemFree(pipe); + return HDF_FAILURE; + } + dev->ctrPipe = pipe; + + return HDF_SUCCESS; +} + +static int32_t FunBulkReadSync(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t *buffer = NULL; + uint8_t interfaceId = 0, pipeId = 0; + int32_t timeout = 0; + uint32_t length = 0; + struct UsbPipeInfo pipe; + struct UsbRequest *request = NULL; + struct UsbRequestParams parmas; + UsbInterfaceHandle *interfaceHandle = NULL; + + memset(&parmas, 0, sizeof(parmas)); + memset(&pipe, 0, sizeof(pipe)); + + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadUint8(data, &interfaceId)){ + HDF_LOGE("%{public}s:%{public}d read interfaceId error", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadUint8(data, &pipeId)){ + HDF_LOGE("%{public}s:%{public}d read pipeId error", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data, &timeout)){ + HDF_LOGE("%{public}s:%{public}d read timeout error", __func__, __LINE__); + return HDF_ERR_IO; + } + ret = GetPipe(port, interfaceId, pipeId, &pipe); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d get pipe info failed interfaceId=%{public}d, pipeId=%{public}d", __func__, __LINE__, interfaceId, pipeId); + return HDF_FAILURE; + } + interfaceHandle = InterfaceIdToHandle(port, pipe.interfaceId); + if (!interfaceHandle){ + HDF_LOGE("%{public}s:%{public}d InterfaceIdToHandle failed interfaceId=%{public}d, pipeId=%{public}d", __func__, __LINE__, interfaceId, pipeId); + return HDF_FAILURE; + } + request = UsbAllocRequest(interfaceHandle, 0, pipe.maxPacketSize); + if (!request) { + HDF_LOGE("%{public}s:%{public}d readReq request faild", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + + HDF_LOGD("%{public}s:%{public}d request:%{public}p ", __func__, __LINE__, request); + //parmas.pipeAddress = pipe.pipeAddress; + parmas.pipeAddress = pipe.pipeDirection | pipe.pipeAddress; + parmas.pipeId = pipe.pipeId; + parmas.interfaceId = pipe.interfaceId; + parmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; + parmas.timeout = timeout; + parmas.dataReq.numIsoPackets = 0; + parmas.dataReq.directon = (pipe.pipeDirection >> USB_DIR_OFFSET) & 0x1; + parmas.dataReq.length = pipe.maxPacketSize; + ret = UsbFillRequest(request, interfaceHandle, &parmas); + if (HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s: UsbFillRequest faile, ret=%{public}d \n", __func__, ret); + goto out; + } + + ret = UsbSubmitRequestSync(request); + if (HDF_SUCCESS != ret) { + HDF_LOGE("UsbSubmitRequestSync faile, ret=%{public}d \n", ret); + ret = HDF_FAILURE; + OsalMSleep(SUBMIT_SLEEP_TIME); + goto out; + } + + length = request->compInfo.actualLength; + buffer = (uint8_t *)OsalMemCalloc(length + 1); + if (buffer == NULL) { + HDF_LOGE("%{public}s: OsalMemCalloc error", __func__); + ret = HDF_ERR_MALLOC_FAIL; + goto err_out; + } + HDF_LOGD("%{public}s:%{public}d fumang buffer:%{public}p-%{public}p-actualLength:%{public}d", __func__, __LINE__, \ + request->compInfo.buffer, (uint8_t *)request->compInfo.buffer, request->compInfo.actualLength); + memcpy_s(buffer, length, (uint8_t *)request->compInfo.buffer, request->compInfo.actualLength); + if (!HdfSbufWriteBuffer(reply, buffer, length)) { + HDF_LOGE("%{public}s: sbuf write buffer failed", __func__); + } + +err_out: + OsalMemFree(buffer); +out: + UsbFreeRequest(request); + return ret; +} + +static int32_t FunBulkWriteSync(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int ret = HDF_FAILURE; + uint8_t *buffer = NULL; + uint8_t interfaceId = 0, pipeId = 0; + int32_t timeout = 0; + uint32_t length = 0; + UsbInterfaceHandle* interfaceHandle = NULL; + struct UsbPipeInfo pipe; + struct UsbRequest *request = NULL; + struct UsbRequestParams parmas = {}; + + memset(&parmas, 0, sizeof(parmas)); + memset(&pipe, 0, sizeof(pipe)); + + HDF_LOGE("%{public}s:%{public}d mangf UsbOpen", __func__, __LINE__); + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadUint8(data, &interfaceId)){ + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadUint8(data, &pipeId)){ + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data, &timeout)){ + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadBuffer(data, (const void**)&buffer, &length)){ + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_IO; + } + + ret = GetPipe(port, interfaceId, pipeId, &pipe); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get pipe info failed interfaceId=%{public}d, pipeId=%{public}d", __func__, interfaceId, pipeId); + return HDF_FAILURE; + } + + interfaceHandle = InterfaceIdToHandle(port, interfaceId); + if (interfaceHandle == NULL) { + HDF_LOGE("%{public}s: get interface handle faild \n", __func__); + return HDF_ERR_INVALID_PARAM; + } + request = UsbAllocRequest(interfaceHandle, 0, pipe.maxPacketSize); + if (!request) { + HDF_LOGE("%{public}s: alloc request faild\n", __func__); + return HDF_ERR_MALLOC_FAIL; + } + + HDF_LOGD("%{public}s:%{public}d fumang debug buffer:%{public}s, length:%{public}d, maxPacketSize:%{public}d \n", __func__, __LINE__, \ + buffer, length, pipe.maxPacketSize); + parmas.interfaceId = pipe.interfaceId; + //parmas.pipeAddress = pipe.pipeAddress; + parmas.pipeAddress = pipe.pipeDirection | pipe.pipeAddress; + parmas.pipeId = pipe.pipeId; + parmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; + parmas.timeout = timeout; + parmas.dataReq.numIsoPackets = 0; + parmas.userData = port; + parmas.dataReq.length = length; + parmas.dataReq.buffer = buffer; + ret = UsbFillRequest(request, interfaceHandle, &parmas); + if (HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s: UsbFillRequest faile, ret=%{public}d \n", __func__, ret); + goto out; + } + HDF_LOGD("%{public}s:%{public}d fumang debug buffer:%{public}s, length:%{public}d, maxPacketSize:%{public}d \n", __func__, __LINE__, \ + buffer, length, pipe.maxPacketSize); + ret = UsbSubmitRequestSync(request); + if (HDF_SUCCESS != ret) { + HDF_LOGE("UsbSubmitRequestSync faile, ret=%{public}d \n", ret); + ret = HDF_FAILURE; + OsalMSleep(SUBMIT_SLEEP_TIME); + } + +out: + UsbFreeRequest(request); + return ret; +} + +static int32_t UsbControlTransfer(struct HostDevice *dev, int32_t requestCmd, int32_t cmdType, int32_t target, int32_t direction, int32_t value, \ + int32_t index, int32_t timeout, uint8_t *buffer, uint32_t *length) +{ + int ret = 0; + struct UsbRequest *request = NULL; + struct UsbControlParams controlParams = {}; + struct UsbRequestParams parmas = {}; + + memset(&controlParams, 0, sizeof(controlParams)); + memset(&parmas, 0, sizeof(parmas)); + + if (NULL == dev || NULL == buffer || length == NULL) { + HDF_LOGE("%{public}s:%{public}d null pointer faild", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + request = UsbAllocRequest(dev->ctrDevHandle, 0, MAX_CONTROL_BUFF_SIZE); + if (!request) { + HDF_LOGE("%{public}s:%{public}d UsbAllocRequest alloc request faild\n", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + HDF_LOGE("%{public}s:%{public}d control args : requestCmd:%{public}d, cmdType:%{public}d, target:%{public}d, \ + direction:%{public}d, value:%{public}d, index:%{public}d, timeout:%{public}d, length:%{public}d reqCompInfoleng:%{public}d", + __func__, __LINE__, requestCmd, \ + cmdType, target, direction, value, index, timeout, *length, request->compInfo.length); + + controlParams.request = (uint8_t)requestCmd; + controlParams.target = (UsbRequestTargetType)target; + controlParams.reqType = (UsbControlRequestType)cmdType; + controlParams.directon = (UsbRequestDirection)direction; + controlParams.value = value; + controlParams.index = index; + controlParams.data = (void *)buffer; + controlParams.size = *length; + parmas.interfaceId = USB_CTRL_INTERFACE_ID; + parmas.pipeAddress = 0; + parmas.pipeId = 0; + parmas.requestType = USB_REQUEST_PARAMS_CTRL_TYPE; + parmas.timeout = timeout; + UsbControlSetUp(&controlParams, &parmas.ctrlReq); + + ret = UsbFillRequest(request, dev->ctrDevHandle, &parmas); + if (HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d UsbFillRequest faile, ret=%{public}d ", __func__, __LINE__, ret); + goto out; + } + + ret = UsbSubmitRequestSync(request); + HDF_LOGE("%{public}s:%{public}d UsbSubmitRequestSync ret=%{public}d ", __func__, __LINE__, ret); + if (HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d UsbSubmitRequestSync faile, ret=%{public}d ", __func__, __LINE__, ret); + OsalMSleep(SUBMIT_SLEEP_TIME); + goto out; + } + + HDF_LOGE("%{public}s:%{public}d control args : requestCmd:%{public}d, cmdType:%{public}d, target:%{public}d, \ + direction:%{public}d, value:%{public}d, index:%{public}d, timeout:%{public}d, length:%{public}d reqCompInfoleng:%{public}d", + __func__, __LINE__, requestCmd, \ + cmdType, target, direction, value, index, timeout, *length, request->compInfo.length); + + if(USB_REQUEST_DIR_FROM_DEVICE == direction) { + HDF_LOGD("%{public}s: fumang debug length=%{public}d, actualLength=%{public}d rlen:=%{public}d", __func__, \ + request->compInfo.length, request->compInfo.actualLength, *length); + memcpy_s(buffer, *length, request->compInfo.buffer, request->compInfo.actualLength); + if(*length > request->compInfo.actualLength) *length = request->compInfo.actualLength; + PrintBuffer("UsbControlTransfer", buffer, *length); + } + +out: + UsbFreeRequest(request); + return ret; +} + +static int32_t FunControlTransfer(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int ret = HDF_FAILURE; + uint8_t *buffer = NULL; + uint32_t length = 0; + int32_t requestCmd = 0, requestType = 0, value = 0, index = 0, timeout = 0; + int32_t target = 0, direction = 0, cmdType = 0; + + HDF_LOGE("%{public}s:%{public}d mangf FunControlTransfer entry", __func__, __LINE__); + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + if (port->ctrDevHandle == NULL) { + HDF_LOGE("%{public}s:%{public}d interface handle is null \n", __func__, __LINE__); + ret = HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadInt32(data, &requestType)){ + HDF_LOGE("%{public}s:%{public}d read param fail", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data, &requestCmd)){ + HDF_LOGE("%{public}s:%{public}d read param fail", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data, &value)){ + HDF_LOGE("%{public}s:%{public}d read param fail", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data, &index)){ + HDF_LOGE("%{public}s:%{public}d read param fail", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data, &timeout)){ + HDF_LOGE("%{public}s:%{public}d read param fail", __func__, __LINE__); + return HDF_ERR_IO; + } + + target = requestType & 0x1F; + direction = (requestType >> 7) & 0x1; + cmdType = (requestType >> 5) & 0x3; + HDF_LOGE("%{public}s:%{public}d direction:%{public}d target:%{public}d cmdType::%{public}d\n", __func__, __LINE__, direction, target, cmdType); + if(direction == USB_REQUEST_DIR_TO_DEVICE) { + if(!HdfSbufReadBuffer(data, (const void**)&buffer, &length)) { + HDF_LOGE("%{public}s:%{public}d mangf hdf sbuf Read failed", __func__, __LINE__); + return HDF_FAILURE; + } + HDF_LOGE("%{public}s:%{public}d HdfSbufReadBuffer length = %{public}d", __func__, __LINE__, length); + } else { + length = MAX_CONTROL_BUFF_SIZE; + buffer = (uint8_t*)OsalMemAlloc(length); + if (buffer == NULL) { + HDF_LOGE("%{public}s:%{public}d OsalMemAlloc faild length = %{public}d", __func__, __LINE__, length); + return HDF_ERR_MALLOC_FAIL; + } + HDF_LOGE("%{public}s:%{public}d OsalMemAlloc length = %{public}d", __func__, __LINE__, length); + } + ret = UsbControlTransfer(port, requestCmd, cmdType, target, direction, value, index, timeout, buffer, &length); + if(ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d UsbControlTransfer faild ret:%{public}d\n", __func__, __LINE__, ret); + } + HDF_LOGE("%{public}s:%{public}d UsbControlTransfer ok length = %{public}d", __func__, __LINE__, length); + if(direction == USB_REQUEST_DIR_FROM_DEVICE) { + PrintBuffer("FunControlTransfer 738", buffer, length); + if ((HDF_SUCCESS == ret) && (!HdfSbufWriteBuffer(reply, buffer, length))) { + HDF_LOGE("%{public}s:%{public}d sbuf write buffer failed", __func__, __LINE__); + } + OsalMemFree(buffer); + } + return ret; +} + +static int32_t UsbdReleaseInterface(struct HostDevice *dev, uint8_t interfaceId) +{ + int32_t ret = HDF_FAILURE; + HDF_LOGE("%{public}s:%{public}d interfaceId:%{public}d dev:%{public}s", __func__, __LINE__, interfaceId, dev?"OK":"NULL"); + if(interfaceId >= USB_MAX_INTERFACES) { + HDF_LOGE("%{public}s:%{public}d interfaceId:%{public}d fail", __func__, __LINE__, interfaceId); + return HDF_ERR_INVALID_PARAM; + } + if(!dev){ + HDF_LOGE("%{public}s:%{public}d dev is null", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + if(dev->devHandle[interfaceId] != NULL) { + ret = UsbCloseInterface(dev->devHandle[interfaceId]); + if(ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: UsbCloseInterface failed id = %{public}d ret:%{public}d", __func__, interfaceId, ret); + return ret; + } + dev->devHandle[interfaceId] = NULL; + } + + if(dev->iface[interfaceId] != NULL) { + ret = UsbReleaseInterface(dev->iface[interfaceId]); + if(ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: UsbReleaseInterface failed id = %{public}d ret:%{public}d", __func__, interfaceId, ret); + return ret; + } + dev->iface[interfaceId] = NULL; + } + + return HDF_SUCCESS; +} + + +static void UsbdReleaseInterfaces(struct HostDevice *dev) +{ + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d: invalid parma", __func__, __LINE__); + return ; + } + + for (int i = 0; i < USB_MAX_INTERFACES; ++i) { + if (dev->iface[i]) { + UsbReleaseInterface(dev->iface[i]); + dev->iface[i] = NULL; + } + } + if (dev->ctrIface) { + UsbReleaseInterface(dev->ctrIface); + dev->ctrIface = NULL; + } +} + +static int32_t UsbdClaimInterface(struct HostDevice *dev, uint8_t interfaceId) +{ + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d: invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + if(interfaceId >= USB_MAX_INTERFACES) { + return HDF_ERR_INVALID_PARAM; + } + + if(dev->iface[interfaceId] == NULL) { + dev->iface[interfaceId] = GetUsbInterfaceById((const struct HostDevice *)dev, interfaceId); + if (dev->iface[interfaceId] == NULL) { + HDF_LOGE("%{public}s:%{public}d UsbClaimInterface failed id = %{public}d, busNum=%{public}d, devAddr=%{public}d", + __func__, __LINE__, interfaceId, (int32_t)dev->busNum, (int32_t)dev->devAddr); + return HDF_FAILURE; + } + } + + if (dev->devHandle[interfaceId] == NULL) { + dev->devHandle[interfaceId] = UsbOpenInterface(dev->iface[interfaceId]); + if (dev->devHandle[interfaceId] == NULL) { + HDF_LOGE("%{public}s:%{public}d UsbOpenInterface failed id = %{public}d", __func__, __LINE__, interfaceId); + return HDF_FAILURE; + } + } + + return HDF_SUCCESS; +} + +static int32_t UsbdClaimInterfaces(struct HostDevice *dev) +{ + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d: invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + // init interface + memset(dev->iface, 0, sizeof(struct UsbInterface *)*USB_MAX_INTERFACES); + + dev->ctrIface = GetUsbInterfaceById((const struct HostDevice *)dev, USB_CTRL_INTERFACE_ID); + if (dev->ctrIface == NULL) { + HDF_LOGE("%{public}s:%{public}d GetUsbInterfaceById null", __func__, __LINE__); + goto error; + } + + return HDF_SUCCESS; + + error: + UsbdReleaseInterfaces(dev); + return HDF_FAILURE; +} + +static void UsbdCloseInterfaces(struct HostDevice *dev) +{ + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d: invalid parma", __func__, __LINE__); + return ; + } + + for (int i = 0; i < USB_MAX_INTERFACES; ++i) { + if (dev->devHandle[i]) { + UsbCloseInterface(dev->devHandle[i]); + dev->devHandle[i] = NULL; + } + } + if (dev->ctrDevHandle) { + UsbCloseInterface(dev->ctrDevHandle); + dev->ctrDevHandle = NULL; + } +} + +static int32_t FunClaimInterface(struct HostDevice *port, struct HdfSBuf *data) +{ + if ((port == NULL) || (data == NULL)) { + HDF_LOGE("%{public}s:%{public}d invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + uint8_t interfaceId; + if (!HdfSbufReadUint8(data, &interfaceId)) { + HDF_LOGE("%{public}s:%{public}d sbuf read interfaceNum failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + return UsbdClaimInterface(port, interfaceId); +} + +static int32_t FunReleaseInterface(struct HostDevice *port, struct HdfSBuf *data) +{ + if ((port == NULL) || (data == NULL)) { + HDF_LOGE("%{public}s:%{public}d invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + uint8_t interfaceId; + if (!HdfSbufReadUint8(data, &interfaceId)) { + HDF_LOGE("%{public}s:%{public}d sbuf read interfaceNum failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGE("%{public}s:%{public}d interfaceId:%{public}d", __func__, __LINE__, interfaceId); + return UsbdReleaseInterface(port, interfaceId); +} + +static int32_t UsbdOpenInterfaces(struct HostDevice *dev) +{ + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d: invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + // init interface handle + memset(dev->devHandle, 0, sizeof(UsbInterfaceHandle *)*USB_MAX_INTERFACES); + + HDF_LOGE("%{public}s:%{public}d UsbOpenInterface start", __func__, __LINE__); + dev->ctrDevHandle = UsbOpenInterface(dev->ctrIface); + HDF_LOGE("%{public}s:%{public}d UsbOpenInterface end", __func__, __LINE__); + if (dev->ctrDevHandle == NULL) { + HDF_LOGE("%{public}s:%{public}d ctrDevHandle UsbOpenInterface null", __func__, __LINE__); + goto error; + } + return HDF_SUCCESS; + +error: + UsbdCloseInterfaces(dev); + return HDF_FAILURE; +} +static int32_t FunGetDeviceDescriptor(struct HostDevice *port, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t *buffer = NULL; + uint32_t length = 0; + + if ((port == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + length = USB_MAX_DESCRIPTOR_SIZE; + buffer = (uint8_t *)OsalMemAlloc(length); + if(!buffer){ + HDF_LOGE("%{public}s:%{public}d malloc fail ", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + ret = UsbControlTransfer(port, USB_DDK_REQ_GET_DESCRIPTOR, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_TARGET_DEVICE, \ + USB_REQUEST_DIR_FROM_DEVICE, (int32_t)USB_DDK_DT_DEVICE << 8, 0, USB_CTRL_SET_TIMEOUT, buffer, &length); + if(HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret); + OsalMemFree(buffer); + return ret; + } + PrintBuffer("FunGetDeviceDescriptor", buffer, length); + if(!HdfSbufWriteBuffer(reply, buffer, length)){ + ret = HDF_ERR_IO; + } + OsalMemFree(buffer); + return ret; +} + +static int32_t FunGetConfigDescriptor(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t *buffer = NULL; + uint32_t length = 0; + uint8_t configId = 0; + + if ((port == NULL) || (reply == NULL) || (data == NULL)) { + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + if (!HdfSbufReadUint8(data, &configId)) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + length = USB_MAX_DESCRIPTOR_SIZE; + buffer = (uint8_t *)OsalMemAlloc(length); + if(!buffer){ + HDF_LOGE("%{public}s:%{public}d malloc fail ", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + ret = UsbControlTransfer(port, USB_DDK_REQ_GET_DESCRIPTOR, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_TARGET_DEVICE, \ + USB_REQUEST_DIR_FROM_DEVICE, ((int32_t)USB_DDK_DT_CONFIG << 8) + configId, 0, USB_CTRL_SET_TIMEOUT, buffer, &length); + if(HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + OsalMemFree(buffer); + return HDF_ERR_IO; + } + PrintBuffer("FunGetConfigDescriptor", buffer, length); + if(!HdfSbufWriteBuffer(reply, buffer, length)){ + ret = HDF_ERR_IO; + } + OsalMemFree(buffer); + return ret; +} + +static int32_t FunGetStringDescriptor(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t *buffer = NULL; + uint32_t length = 0; + uint8_t stringId = 0; + + if ((port == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + + if (!HdfSbufReadUint8(data, &stringId)) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + length = USB_MAX_DESCRIPTOR_SIZE; + buffer = (uint8_t *)OsalMemAlloc(length); + if(!buffer){ + HDF_LOGE("%{public}s:%{public}d malloc fail ", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + ret = UsbControlTransfer(port, USB_DDK_REQ_GET_DESCRIPTOR, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_TARGET_DEVICE, \ + USB_REQUEST_DIR_FROM_DEVICE, ((int32_t)USB_DDK_DT_STRING << 8) + stringId, 0, USB_CTRL_SET_TIMEOUT, buffer, &length); + if(HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + OsalMemFree(buffer); + return HDF_ERR_IO; + } + PrintBuffer("FunGetStringDescriptor", buffer, length); + if(!HdfSbufWriteBuffer(reply, buffer, length)){ + ret = HDF_ERR_IO; + } + OsalMemFree(buffer); + return ret; +} + + +static int32_t FunGetActiveConfig(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t configId = 0; + uint32_t length = 1; + + HDF_LOGE("%{public}s: mangf FunControlTransfer entry", __func__); + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + ret = UsbControlTransfer(port, USB_DDK_REQ_GET_CONFIGURATION, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_TARGET_DEVICE, \ + USB_REQUEST_DIR_FROM_DEVICE, 0, 0, USB_CTRL_SET_TIMEOUT, &configId, &length); + if(HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufWriteUint8(reply, configId)){ + ret = HDF_ERR_IO; + } + return ret; +} +static int32_t FunSetActiveConfig(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t configId = 0; + uint32_t length = 1; + + HDF_LOGE("%{public}s: mangf FunControlTransfer entry", __func__); + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (!HdfSbufReadUint8(data, &configId)) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + ret = UsbControlTransfer(port, USB_DDK_REQ_SET_CONFIGURATION, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_TARGET_DEVICE, \ + USB_REQUEST_DIR_TO_DEVICE, 0, 0, USB_CTRL_SET_TIMEOUT, &configId, &length); + if(HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_IO; + } + + return ret; +} + +static int32_t FunSetInterface(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t interfaceId = 0, altIndex = 0; + uint32_t length = 1; + + HDF_LOGE("%{public}s:%{public}d mangf FunControlTransfer entry", __func__, __LINE__); + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadUint8(data, &interfaceId)){ + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + if (!HdfSbufReadUint8(data, &altIndex)) { + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + ret = UsbControlTransfer(port, USB_DDK_REQ_SET_INTERFACE, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_TARGET_DEVICE, \ + USB_REQUEST_DIR_TO_DEVICE, 0, 0, USB_CTRL_SET_TIMEOUT, &altIndex, &length); + if(HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret); + return HDF_ERR_IO; + } + return ret; +} + +static int32_t FunBulkRead(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t interfaceId = 0, pipeId = 0; + int32_t timeout = 0; + struct UsbPipeInfo pipe; + struct RequestMsg *reqMsg = NULL; + struct UsbRequestParams parmas; + UsbInterfaceHandle* interfaceHandle = NULL; + memset(&pipe, 0, sizeof(pipe)); + memset(&parmas, 0, sizeof(parmas)); + + if ((port == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadUint8(data, &interfaceId)){ + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadUint8(data, &pipeId)){ + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadInt32(data, &timeout)){ + HDF_LOGE("%{public}s:%{public}d failed", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + ret = GetPipe(port, interfaceId, pipeId, &pipe); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get pipe info failed", __func__); + return HDF_FAILURE; + } + interfaceHandle = InterfaceIdToHandle(port, pipe.interfaceId); + if(interfaceHandle == NULL) { + HDF_LOGE("%{public}s:%{public}d InterfaceIdToHandle failed bus:%{public}d devAddr:%{public}d interfaceId:%{public}d", + __func__, __LINE__, port->busNum, port->devAddr, pipe.interfaceId); + return HDF_FAILURE; + } + reqMsg = UsbdAllocRequestMsg(interfaceHandle, 0, pipe.maxPacketSize); + if(reqMsg == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + parmas.userData = (void *)port; + //parmas.pipeAddress = pipe.pipeAddress; + parmas.pipeAddress = pipe.pipeDirection | pipe.pipeAddress; + parmas.pipeId = pipe.pipeId; + parmas.interfaceId = pipe.interfaceId; + parmas.callback = UsbdReadCallback; + parmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; + parmas.timeout = timeout; + parmas.dataReq.numIsoPackets = 0; + parmas.dataReq.directon = (pipe.pipeDirection >> USB_PIPE_DIR_OFFSET) & 0x1; + parmas.dataReq.length = pipe.maxPacketSize; + ret = UsbFillRequest(reqMsg->request, interfaceHandle, &parmas); + if (HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s: UsbFillRequest faile, ret=%{public}d \n", __func__, ret); + goto error; + } + + OsalMutexLock(&port->requestLock); + struct UsbIfRequest *ifReq = (struct UsbIfRequest *)reqMsg->request; + struct UsbHostRequest *hostReq = (struct UsbHostRequest *)ifReq->hostRequest; + OsalSemInit(&hostReq->sem, 0); + ret = UsbSubmitRequestAsync(reqMsg->request); + if (HDF_SUCCESS != ret) { + HDF_LOGE("UsbSubmitRequestAsync faile, ret=%{public}d \n", ret); + ret = HDF_FAILURE; + OsalMutexUnlock(&port->requestLock); + OsalMSleep(SUBMIT_SLEEP_TIME); + goto error; + } + HdfSListAdd(&port->requestQueue, &reqMsg->node); + OsalMutexUnlock(&port->requestLock); + +error: + UsbdFreeRequestMsg(reqMsg); + return ret; +} + +static int32_t FunBulkWrite(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_FAILURE; + uint8_t interfaceId = 0, pipeId = 0; + int32_t timeout = 0; + uint32_t length = 0; + uint8_t *buffer = NULL; + struct UsbPipeInfo pipe; + struct RequestMsg *reqMsg = NULL; + struct UsbRequestParams parmas; + UsbInterfaceHandle* interfaceHandle = NULL; + memset(&pipe, 0, sizeof(pipe)); + memset(&parmas, 0, sizeof(parmas)); + + if ((port == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadUint8(data, &interfaceId)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadUint8(data, &pipeId)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadInt32(data, &timeout)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadBuffer(data, (const void**)&buffer, &length)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + ret = GetPipe(port, interfaceId, pipeId, &pipe); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get pipe info failed", __func__); + return HDF_FAILURE; + } + interfaceHandle = InterfaceIdToHandle(port, pipe.interfaceId); + if(interfaceHandle == NULL) { + return HDF_FAILURE; + } + reqMsg = UsbdAllocRequestMsg(interfaceHandle, 0, pipe.maxPacketSize); + if(reqMsg == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + + parmas.userData = (void *)port; + //parmas.pipeAddress = pipe.pipeAddress; + parmas.pipeAddress = pipe.pipeDirection | pipe.pipeAddress; + parmas.pipeId = pipe.pipeId; + parmas.interfaceId = pipe.interfaceId; + parmas.callback = UsbdWriteCallback; + parmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; + parmas.timeout = timeout; + parmas.dataReq.numIsoPackets = 0; + parmas.dataReq.directon = (pipe.pipeDirection >> USB_PIPE_DIR_OFFSET) & 0x1; + parmas.dataReq.length = length; + parmas.dataReq.buffer = buffer; + ret = UsbFillRequest(reqMsg->request, interfaceHandle, &parmas); + if (HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s: UsbFillRequest faile, ret=%{public}d \n", __func__, ret); + goto error; + } + + OsalMutexLock(&port->requestLock); + struct UsbIfRequest *ifReq = (struct UsbIfRequest *)reqMsg->request; + struct UsbHostRequest *hostReq = (struct UsbHostRequest *)ifReq->hostRequest; + OsalSemInit(&hostReq->sem, 0); + ret = UsbSubmitRequestAsync(reqMsg->request); + if (HDF_SUCCESS != ret) { + HDF_LOGE("UsbSubmitRequestAsync faile, ret=%{public}d \n", ret); + ret = HDF_FAILURE; + OsalMutexUnlock(&port->requestLock); + OsalMSleep(SUBMIT_SLEEP_TIME); + goto error; + } + HdfSListAdd(&port->requestQueue, &reqMsg->node); + OsalMutexUnlock(&port->requestLock); + +error: + UsbdFreeRequestMsg(reqMsg); + return HDF_SUCCESS; +} + +static int32_t FunRequestQueue(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = 0; + struct UsbPipeInfo pipe; + struct RequestMsg *reqMsg = NULL; + struct UsbRequestParams parmas; + UsbInterfaceHandle* interfaceHandle = NULL; + uint8_t interfaceId = 0, pipeId = 0; + uint8_t *clientData = NULL, *buffer = NULL; + uint32_t clientLength = 0, length = 0; + bool bWrite = false; + int32_t requestCount = 0; + memset(&pipe, 0, sizeof(pipe)); + memset(&parmas, 0, sizeof(parmas)); + + if ((port == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadUint8(data, &interfaceId)){ + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadUint8(data, &pipeId)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadBuffer(data, (const void**)&clientData, &clientLength)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadBuffer(data, (const void**)&buffer, &length)) { + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + PrintBuffer("clientData", clientData, clientLength); + PrintBuffer("buffer", buffer, length); + //interfaceId = 0; + ret = GetPipe(port, interfaceId, pipeId, &pipe); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: get pipe info failed, interfaceId=%{public}d,pipeId=%{public}d pipeAddr:%{public}d", __func__, interfaceId, pipeId, pipe.pipeAddress); + return HDF_FAILURE; + } + interfaceHandle = InterfaceIdToHandle(port, pipe.interfaceId); + if(interfaceHandle == NULL) { + return HDF_FAILURE; + } + reqMsg = UsbdAllocRequestMsg(interfaceHandle, 0, pipe.maxPacketSize); + if(reqMsg == NULL) { + return HDF_ERR_MALLOC_FAIL; + } + reqMsg->clientData = clientData; + reqMsg->clientLength = clientLength; + reqMsg->request->compInfo.status = -1; + bWrite = (pipe.pipeDirection == USB_PIPE_DIRECTION_OUT); + parmas.interfaceId = pipe.interfaceId; + //parmas.pipeAddress = pipe.pipeAddress; + parmas.pipeAddress = pipe.pipeDirection | pipe.pipeAddress; + parmas.pipeId = pipe.pipeId; + parmas.requestType = USB_REQUEST_PARAMS_DATA_TYPE; + parmas.timeout = USB_CTRL_SET_TIMEOUT; + parmas.dataReq.numIsoPackets = 0; + parmas.userData = (void *)port; + parmas.dataReq.length = length; + HDF_LOGE("%{public}s: interfaceId=%{public}d,pipeId=%{public}d bwrite:%{public}d pipDriect:%{public}d pipeAddr:%{public}d Addr:%{public}d", + __func__, interfaceId, pipeId, bWrite, pipe.pipeDirection, pipe.pipeAddress, parmas.pipeAddress); + if(bWrite) { + parmas.callback = UsbdWriteCallback; + parmas.dataReq.buffer = buffer; + } else { + parmas.callback = UsbdReadCallback; + parmas.dataReq.directon = (pipe.pipeDirection >> USB_PIPE_DIR_OFFSET) & 0x1; + } + ret = UsbFillRequest(reqMsg->request, interfaceHandle, &parmas); + if (HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s: fumang UsbFillRequest faile, ret=%{public}d \n", __func__, ret); + goto error; + } + HDF_LOGE("%{public}s: interfaceId=%{public}d,pipeId=%{public}d bwrite:%{public}d pipDriect:%{public}d pmdir:%{public}d ", + __func__, interfaceId, pipeId, bWrite, pipe.pipeDirection, parmas.dataReq.directon); + OsalMutexLock(&port->requestLock); + struct UsbIfRequest *ifReq = (struct UsbIfRequest *)reqMsg->request; + struct UsbHostRequest *hostReq = (struct UsbHostRequest *)ifReq->hostRequest; + OsalSemInit(&hostReq->sem, 0); + ret = UsbSubmitRequestAsync(reqMsg->request); + if (HDF_SUCCESS != ret) { + HDF_LOGE("fumang UsbSubmitRequestAsync faile, ret=%{public}d \n", ret); + ret = HDF_FAILURE; + OsalMutexUnlock(&port->requestLock); + OsalMSleep(SUBMIT_SLEEP_TIME); + goto error; + } + HdfSListAddTail(&port->requestQueue, &reqMsg->node); + requestCount = HdfSListCount(&port->requestQueue); + OsalMutexUnlock(&port->requestLock); + HDF_LOGE("%{public}s:%{public}d fumang send request %{public}p ok, count = %{public}d", __func__, __LINE__, reqMsg, requestCount); + return HDF_SUCCESS; +error: + UsbdFreeRequestMsg(reqMsg); + return ret; +} +static int32_t FunRequestWait(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_SUCCESS; + struct RequestMsg *reqMsg = NULL; + struct HdfSListNode *requestNode = NULL; + uint8_t *buffer = NULL; + uint32_t length = 0; + int32_t timeout = 0; + int32_t requestCount = 0; + // int32_t status = -1; + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadInt32(data, &timeout)){ + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGE("%{public}s:%{public}d request wait entry ok timeout = %{public}d", __func__, __LINE__, timeout); + + // 获取第一个请求,查询状态,并等待完成 + OsalMutexLock(&port->requestLock); + if(!HdfSListIsEmpty(&port->requestQueue)) { + requestNode = HdfSListPop(&port->requestQueue); + requestCount = HdfSListCount(&port->requestQueue); + } + OsalMutexUnlock(&port->requestLock); + if(requestNode == NULL) { + HDF_LOGE("%{public}s:%{public}d fumang request node is null", __func__, __LINE__); + return HDF_FAILURE; + } + reqMsg = HDF_SLIST_CONTAINER_OF(struct HdfSListNode, requestNode, struct RequestMsg, node); + if(reqMsg == NULL) { + HDF_LOGE("%{public}s:%{public}d fumang request msg is null", __func__, __LINE__); + return HDF_FAILURE; + } + + HDF_LOGE("%{public}s:%{public}d fumang get request %{public}p ok, count=%{public}d status:%{public}d", __func__, __LINE__, reqMsg, requestCount, reqMsg->request->compInfo.status); + // when request is not completed, waitting for it + if((int32_t)(reqMsg->request->compInfo.status) == -1) { + HDF_LOGE("%{public}s:%{public}d fumang request waitting... timeout:%{public}d", __func__, __LINE__, timeout); + //struct UsbIfRequest *treq = (struct UsbIfRequest *)reqMsg->request) + ret = OsalSemWait(&((struct UsbIfRequest *)reqMsg->request)->hostRequest->sem, timeout); + HDF_LOGE("%{public}s:%{public}d wait over ret:%{public}d timeout:%{public}d status:%{public}d", + __func__, __LINE__, ret, timeout, reqMsg->request->compInfo.status); + ret = (int32_t)(reqMsg->request->compInfo.status); + if((ret == USB_REQUEST_COMPLETED) || (USB_REQUEST_COMPLETED_SHORT == ret)){ + ret = HDF_SUCCESS; + } + if(HDF_SUCCESS != ret) { + HDF_LOGE("%{public}s:%{public}d fumang request wait failed ret = %{public}d", __func__, __LINE__, ret); + goto error; + } + } + + length = DataFifoLen(&port->readFifo); + HDF_LOGE("%{public}s:%{public}d len:%{public}d", __func__, __LINE__, length); + buffer = (uint8_t *)OsalMemCalloc(length + 1); + if (buffer == NULL) { + HDF_LOGE("%{public}s:%{public}d OsalMemCalloc error", __func__, __LINE__); + ret = HDF_ERR_MALLOC_FAIL; + goto error; + } + memset(buffer, 0, length+1); + OsalMutexLock(&port->readLock); + length = DataFifoRead(&port->readFifo, buffer, DataFifoLen(&port->readFifo)); + HDF_LOGE("%{public}s:%{public}d len:%{public}d", __func__, __LINE__, length); + if (length == 0) { + HDF_LOGE("%{public}s:%{public}d fumang request read no data", __func__, __LINE__); + } + OsalMutexUnlock(&port->readLock); + if(!HdfSbufWriteBuffer(reply, reqMsg->clientData, reqMsg->clientLength)) { + ret = HDF_ERR_IO; + HDF_LOGE("%{public}s:%{public}d fumang HdfSbufWriteBuffer failed", __func__, __LINE__); + goto out; + } + if(length > 0) { + if(!HdfSbufWriteBuffer(reply, buffer, length)){ + ret = HDF_ERR_IO; + HDF_LOGE("%{public}s:%{public}d fumang HdfSbufWriteBuffer failed", __func__, __LINE__); + goto out; + } + PrintBuffer("fumang request wait get data:", buffer, length); + } else { + if(!HdfSbufWriteUint32(reply, length)){ + ret = HDF_ERR_IO; + HDF_LOGE("%{public}s:%{public}d fumang HdfSbufWriteBuffer failed", __func__, __LINE__); + goto out; + } + } + HDF_LOGE("%{public}s:%{public}d fumang request wait success, buffer=%{public}p, length=%{public}d", __func__, __LINE__, buffer, length); +out: + OsalMemFree(buffer); +error: + UsbdFreeRequestMsg(reqMsg); + return ret; +} + +static int32_t FunRequestCancel(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int32_t ret = HDF_SUCCESS; + struct RequestMsg *reqMsg = NULL; + struct HdfSListNode *requestNode = NULL; + uint8_t interfaceId = 0, endpointId = 0; + + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s: invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if(!HdfSbufReadUint8(data, &interfaceId)){ + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + if(!HdfSbufReadUint8(data, &endpointId)){ + HDF_LOGE("%{public}s: mangf invalid parma", __func__); + return HDF_ERR_INVALID_PARAM; + } + + // 获取第一个请求,取消请求 + OsalMutexLock(&port->requestLock); + if(!HdfSListIsEmpty(&port->requestQueue)) { + requestNode = HdfSListPop(&port->requestQueue); + } + OsalMutexUnlock(&port->requestLock); + if(requestNode == NULL) { + HDF_LOGE("%{public}s:%{public}d request node is null", __func__, __LINE__); + return HDF_FAILURE; + } + reqMsg = HDF_SLIST_CONTAINER_OF(struct HdfSListNode, requestNode, struct RequestMsg, node); + if(reqMsg == NULL) { + HDF_LOGE("%{public}s:%{public}d request msg is null", __func__, __LINE__); + return HDF_FAILURE; + } + //PrintBuffer("FunRequestWait.clientData", reqMsg->clientData, reqMsg->clientLength); + ret = UsbCancelRequest(reqMsg->request); + if(ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d request cancel failed = %{public}d", __func__, __LINE__, ret); + } + UsbdFreeRequestMsg(reqMsg); + return ret; +} + +static int32_t UsbdInit(struct HostDevice *dev) +{ + int32_t ret = HDF_FAILURE; + struct UsbSession *session = NULL; + HDF_LOGE("%{public}s:%{public}d mangf UsbdInit", __func__, __LINE__); + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d: invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + HDF_LOGE("%{public}s:%{public}d mangf UsbdInit", __func__, __LINE__); + if (dev->initFlag == true) { + HDF_LOGE("%{public}s:%{public}d: mangf initFlag is true", __func__, __LINE__); + return HDF_SUCCESS; + } + HDF_LOGE("%{public}s:%{public}d mangf UsbdInit", __func__, __LINE__); + ret = UsbInitHostSdk(NULL); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d mangf UsbInitHostSdk faild", __func__, __LINE__); + return HDF_ERR_IO; + } + if(dev->service) + dev->service->session = session; + + HDF_LOGE("%{public}s:%{public}d UsbdClaimInterfaces start", __func__, __LINE__); + ret = UsbdClaimInterfaces(dev); + HDF_LOGE("%{public}s:%{public}d UsbdClaimInterfaces end ret:%{public}d", __func__, __LINE__, ret); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d mangf UsbdClaimInterfaces faild ret:%{public}d", __func__, __LINE__, ret); + goto error_claim_interfaces; + } + + HDF_LOGE("%{public}s:%{public}d UsbdOpenInterfaces start", __func__, __LINE__); + ret = UsbdOpenInterfaces(dev); + HDF_LOGE("%{public}s:%{public}d UsbdOpenInterfaces end ret:%{public}d", __func__, __LINE__, ret); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d mangf UsbdOpenInterfaces faild ret:%{public}d", __func__, __LINE__, ret); + goto error_open_interfaces; + } + + HDF_LOGE("%{public}s:%{public}d UsbdClaimInterfaces start", __func__, __LINE__); + ret = UsbdGetCtrlPipe(dev); + HDF_LOGE("%{public}s:%{public}d UsbdGetCtrlPipe end ret:%{public}d", __func__, __LINE__, ret); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d mangf UsbdGetPipes failed ret:%{public}d", __func__, __LINE__, ret); + goto error_get_pipes; + } + + dev->initFlag = true; + HDF_LOGD("%{public}s:%{public}d========OK busNum:%{public}d devAddr:%{public}d", __func__, __LINE__, dev->busNum, dev->devAddr); + return HDF_SUCCESS; + +error_get_pipes: + UsbdCloseInterfaces(dev); +error_open_interfaces: + UsbdReleaseInterfaces(dev); +error_claim_interfaces: + UsbExitHostSdk(dev->service->session); + HDF_LOGD("%{public}s:%{public}d UsbExitHostSdk session:%{public}s ", __func__, __LINE__, dev->service->session ? "OK":"NULL"); + dev->service->session = NULL; + return ret; +} + +static void UsbdRelease(struct HostDevice *dev) +{ + if (dev == NULL) { + HDF_LOGE("%{public}s:%{public}d: invalid parma", __func__, __LINE__); + return ; + } + + if (dev->initFlag == false) { + HDF_LOGE("%{public}s:%{public}d: initFlag is false", __func__, __LINE__); + return; + } + + UsbdFreeCtrlPipe(dev); + UsbdCloseInterfaces(dev); + UsbdReleaseInterfaces(dev); + UsbExitHostSdk(dev->service->session); + HDF_LOGD("%{public}s:%{public}d UsbExitHostSdk session:%{public}s ", __func__, __LINE__, dev->service->session ? "OK":"NULL"); + dev->service->session = NULL; + OsalMutexDestroy(&dev->writeLock); + OsalMutexDestroy(&dev->readLock); + OsalMutexDestroy(&dev->lock); + OsalMutexDestroy(&dev->requestLock); + dev->busNum = 0; + dev->devAddr = 0; + dev->initFlag = false; +} + +static int32_t FunOpenDevice(struct HostDevice *port, struct HdfSBuf *data, struct HdfSBuf *reply) +{ + int ret; + HDF_LOGE("%{public}s:%{public}d mangf UsbOpen", __func__, __LINE__); + if ((port == NULL) || (data == NULL) || (reply == NULL)) { + HDF_LOGE("%{public}s:%{public}d mangf invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + if(port->initFlag){ + HDF_LOGE("%{public}s:%{public}d device is already on flag:%{public}d bus:%{public}d dev:%{public}d", __func__, __LINE__, + port->initFlag, port->busNum, port->devAddr); + if(reply) HdfSbufWriteInt32(reply, 1); + return HDF_SUCCESS; + } + ret = UsbdInit(port); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d mangf UsbInit failed ret:%{public}d", __func__, __LINE__, ret); + goto err_init; + } + ret = UsbdAllocFifo(&port->readFifo, READ_BUF_SIZE); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d mangf UsbAllocFifo failed ret:%{public}d", __func__, __LINE__, ret); + ret = HDF_ERR_INVALID_PARAM; + goto err_fifo; + } +// if(!HdfSbufWriteInt32(reply, 1)){ +// ret = HDF_ERR_IO; +// HDF_LOGE("%{public}s:%{public}d mangf UsbOpen write fd error", __func__, __LINE__); +// //goto err_fifo; +// } + HDF_LOGE("%{public}s:%{public}d mangf UsbOpen success", __func__, __LINE__); + OsalMSleep(OPEN_SLEPP_TIME); + return HDF_SUCCESS; + +err_fifo: + UsbdFreeFifo(&port->readFifo); +err_init: + UsbdRelease(port); + return ret; +} + +static int32_t FunCloseDevice(struct HostDevice *port, struct HdfSBuf *data) +{ + if ((port == NULL) || (data == NULL)) { + HDF_LOGE("%{public}s:%{public}d invalid parma", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + if(!port->initFlag){ + HDF_LOGE("%{public}s:%{public}d device already close", __func__, __LINE__); + return HDF_SUCCESS; + } + UsbdFreeFifo(&port->readFifo); + UsbdRelease(port); + RemoveDevFromService(port->service, port); + OsalMemFree(port); + port = NULL; + return HDF_SUCCESS; +} + +static int32_t FunSetRole(struct HdfSBuf *data, struct HdfSBuf *reply, struct UsbdService *service) +{ + int32_t portId = 0; + int32_t powerRole= 0; + int32_t dataRole= 0; + if(!HdfSbufReadInt32(data,&portId)){ + HDF_LOGE("%{public}s:%{public}d mangf Read data faild", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data,&powerRole)){ + HDF_LOGE("%{public}s:%{public}d mangf Read data faild", __func__, __LINE__); + return HDF_ERR_IO; + } + if(!HdfSbufReadInt32(data,&dataRole)){ + HDF_LOGE("%{public}s:%{public}d mangf Read data faild", __func__, __LINE__); + return HDF_ERR_IO; + } + + HDF_LOGE("%{public}s:%{public}d FunSetRole %{public}d %{public}d %{public}d", __func__, __LINE__, portId, powerRole, dataRole); + int32_t ret = SetPort( portId, powerRole, dataRole, service); + if (ret) + { + HDF_LOGE("%{public}s:%{public}d FunSetRole fasle", __func__, __LINE__); + return HDF_ERR_IO; + } + if(reply) HdfSbufWriteInt32(reply, ret); + return HDF_SUCCESS; +} + +static int32_t FunQueryPort(struct HdfSBuf *data, struct HdfSBuf *reply, struct UsbdService *service){ + int32_t portId = 0; + int32_t powerRole= 0; + int32_t dataRole= 0; + int32_t mode = 0; + int32_t ret = QueryPort(&portId, &powerRole, &dataRole, &mode, service); + if (ret) + { + HDF_LOGE("%{public}s:%{public}d FunQueryPort fasle", __func__, __LINE__); + return HDF_ERR_IO; + } + if(reply){ + HdfSbufWriteInt32(reply,portId); + HdfSbufWriteInt32(reply,powerRole); + HdfSbufWriteInt32(reply,dataRole); + HdfSbufWriteInt32(reply,mode); + } + return HDF_SUCCESS; +} + +static int32_t FunGetCurrentFunctions(struct HdfSBuf *data, struct HdfSBuf *reply) { + int32_t funcs = UsbdGetFunction(); + HDF_LOGE("%{public}s:%{public}d FunSetCurrentFunctions funcs: %{public}d", __func__, __LINE__, funcs); + if(reply) HdfSbufWriteInt32(reply, funcs); + return HDF_SUCCESS; +} + +static int32_t FunSetCurrentFunctions(struct HdfSBuf *data, struct HdfSBuf *reply) { + int32_t funcs = 0; + HdfSbufReadInt32(data, &funcs); + HDF_LOGE("%{public}s:%{public}d FunSetCurrentFunctions funcs: %{public}d", __func__, __LINE__, funcs); + int ret = UsbdSetFunction(funcs); + if(ret){ + return HDF_ERR_IO; + } + if(reply) HdfSbufWriteInt32(reply, 1); + return HDF_SUCCESS; +} + +static int32_t DispatchBindUsbSubscriber(struct UsbdService *service, struct HdfSBuf *data) +{ + struct UsbdSubscriber *subscriber = NULL; + HDF_LOGE("%{public}s:%{public}d fumang entry", __func__, __LINE__); + if(service == NULL || data == NULL){ + HDF_LOGW("%{public}s:%{public}d param is NULL", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + struct HdfRemoteService *remoteService = HdfSBufReadRemoteService(data); + if (remoteService == NULL) { + HDF_LOGW("%{public}s: remoteService is NULL", __func__); + return HDF_ERR_INVALID_PARAM; + } + + subscriber = (struct UsbdSubscriber *)OsalMemCalloc(sizeof(struct UsbdSubscriber)); + if (subscriber == NULL) { + HDF_LOGE("%{public}s: calloc subscriber error", __func__); + return HDF_ERR_MALLOC_FAIL; + } + memset(subscriber, 0, sizeof(struct UsbdSubscriber)); + subscriber->remoteService = remoteService; + return BindUsbSubscriber(service, subscriber); +} + +static int32_t DispatchUnbindUsbSubscriber(struct UsbdService *service) +{ + if(service == NULL){ + HDF_LOGW("%{public}s:%{public}d param is NULL", __func__, __LINE__); + return HDF_ERR_INVALID_PARAM; + } + return UnbindUsbSubscriber(service); +} + + +int32_t UsbdServiceDispatch(struct HdfDeviceIoClient *client, int cmd, + struct HdfSBuf *data, struct HdfSBuf *reply) +{ + struct UsbdService *service = NULL; + struct HostDevice *port = NULL; + + if (client == NULL) { + HDF_LOGE("%{public}s:%{public}d client is NULL", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + if (client->device == NULL) { + HDF_LOGE("%{public}s:%{public}d client->device is NULL", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + if (client->device->service == NULL) { + HDF_LOGE("%{public}s:%{public}d client->device->service is NULL", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + + HDF_LOGE("%{public}s:%{public}d usbd dispatch cmd = %{public}d", __func__, __LINE__, cmd); + + service = (struct UsbdService *)client->device->service; + if (cmd != CMD_FUN_GET_CURRENT_FUNCTIONS && cmd != CMD_FUN_SET_CURRENT_FUNCTIONS + && cmd != CMD_SET_ROLE && cmd != CMD_QUERY_PORT && CMD_BIND_USB_SUBSCRIBER != cmd + && CMD_UNBIND_USB_SUBSCRIBER != cmd) { + //ParseDeviceBuf(data, &service->busNum, &service->devAddr); + uint8_t busNum = 0; + uint8_t devAddr = 0; + int32_t ret = 0; + ret = ParseDeviceBuf(data, &busNum, &devAddr); + if(ret != HDF_SUCCESS){ + HDF_LOGE("%{public}s:%{public}d cmd = %{public}d ParseDeviceBuf error:%{public}d", __func__, __LINE__, cmd, ret); + return ret; + } + port = FindDevFromService(service, busNum, devAddr); + switch(cmd){ + case CMD_FUN_OPEN_DEVICE: + //HDF_LOGE("%{public}s:%{public}d open device cmd = %{public}d flag:%{public}d busNum:%{public}d devAddr:%{public}d Obus:%{public}d Odev:%{public}d", + // __func__, __LINE__, cmd, service->initFlag, busNum, devAddr, service->busNum, service->devAddr); + if(!port){ + ret = HostDeviceCreate(&port); + HDF_LOGE("%{public}s:%{public}d OpenDevice create ret:%{public}d busNum:%{public}d devAddr:%{public}d port:%{public}p", __func__, __LINE__, ret, busNum, devAddr, port); + if((HDF_SUCCESS == ret) && (port)){ + port->service = service; + port->busNum = busNum; + port->devAddr = devAddr; + OsalMutexLock(&service->lock); + HdfSListAdd(&service->devList, &port->node); + OsalMutexUnlock(&service->lock); + }else{ + return ret; + } + } + break; + default: + if(!port){ + HDF_LOGE("%{public}s:%{public}d cmd = %{public}d busNum:%{public}d devAddr:%{public}d no device", __func__, __LINE__, cmd, busNum, devAddr); + return HDF_DEV_ERR_NO_DEVICE; + } + break; + + } + } + HDF_LOGE("%{public}s:%{public}d cmd = %{public}d port:%{public}p", __func__, __LINE__, cmd, port); + switch (cmd) { + case CMD_BIND_USB_SUBSCRIBER: + return DispatchBindUsbSubscriber(service, data); + case CMD_UNBIND_USB_SUBSCRIBER: + return DispatchUnbindUsbSubscriber(service); + case CMD_FUN_OPEN_DEVICE: + return FunOpenDevice(port, data, reply); + case CMD_FUN_CLOSE_DEVICE: + return FunCloseDevice(port, data); + case CMD_FUN_SEND_BULK_READ_ASYNC: + return FunBulkRead(port, data, reply); + case CMD_FUN_SEND_BULK_WRITE_ASYNC: + return FunBulkWrite(port, data, reply); + case CMD_FUN_SEND_BULK_READ_SYNC: + case CMD_FUN_SEND_INTERRUPT_READ_SYNC: + case CMD_FUN_SEND_ISO_READ_SYNC: + return FunBulkReadSync(port, data, reply); + case CMD_FUN_SEND_BULK_WRITE_SYNC: + case CMD_FUN_SEND_INTERRUPT_WRITE_SYNC: + case CMD_FUN_SEND_ISO_WRITE_SYNC: + return FunBulkWriteSync(port, data, reply); + case CMD_FUN_SEND_CTRL_REQUEST_SYNC: + return FunControlTransfer(port, data, reply); + case CMD_FUN_GET_DEVICE_DESCRIPTOR: + return FunGetDeviceDescriptor(port, reply); + case CMD_FUN_GET_CONFIG_DESCRIPTOR: + return FunGetConfigDescriptor(port, data, reply); + case CMD_FUN_GET_STRING_DESCRIPTOR: + return FunGetStringDescriptor(port, data, reply); + case CMD_FUN_REQUEST_QUEUE: + return FunRequestQueue(port, data, reply); + case CMD_FUN_REQUEST_WAIT: + return FunRequestWait(port, data, reply); + case CMD_FUN_REQUEST_CANCEL: + return FunRequestCancel(port, data, reply); + case CMD_FUN_CLAIM_INTERFACE: + return FunClaimInterface(port, data); + case CMD_FUN_RELEASE_INTERFACE: + return FunReleaseInterface(port, data); + case CMD_FUN_SET_CONFIG: + return FunSetActiveConfig(port, data, reply); + case CMD_FUN_GET_CONFIG: + return FunGetActiveConfig(port, data, reply); + case CMD_FUN_SET_INTERFACE: + return FunSetInterface(port, data, reply); + case CMD_SET_ROLE: + return FunSetRole(data, reply, service); + case CMD_QUERY_PORT: + return FunQueryPort(data, reply,service); + case CMD_FUN_GET_CURRENT_FUNCTIONS: + return FunGetCurrentFunctions(data, reply); + case CMD_FUN_SET_CURRENT_FUNCTIONS: + return FunSetCurrentFunctions(data, reply); + default: + return HDF_ERR_NOT_SUPPORT; + } + + return HDF_SUCCESS; +} + +static int32_t HostDeviceInit(struct HostDevice *port) +{ + if(!port){ + HDF_LOGE("%{public}s:%{public}d param failed", __func__, __LINE__); + return HDF_ERR_INVALID_OBJECT; + } + port->busNum = 0; + port->devAddr = 0; + port->initFlag = false; + port->interfaceCnt = 0; + + if (OsalMutexInit(&port->lock) != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d init lock fail!", __func__, __LINE__); + return HDF_FAILURE; + } + if (OsalMutexInit(&port->requestLock) != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d init lock fail!", __func__, __LINE__); + return HDF_FAILURE; + } + if (OsalMutexInit(&port->writeLock) != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d init lock fail!", __func__, __LINE__); + return HDF_FAILURE; + } + if (OsalMutexInit(&port->readLock) != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d init lock fail!", __func__, __LINE__); + return HDF_FAILURE; + } + HdfSListInit(&port->requestQueue); + //OsalSemInit(&port->requestSem, 0); + return HDF_SUCCESS; +} + + + + +int32_t HostDeviceCreate(struct HostDevice **port) +{ + int32_t ret = HDF_SUCCESS; + struct HostDevice *tmp = NULL; + if(!port){ + return HDF_FAILURE; + } + tmp = (struct HostDevice *)OsalMemCalloc(sizeof(struct HostDevice)); + if(!tmp){ + HDF_LOGE("%{public}s:%{public}d Alloc usb host device failed", __func__, __LINE__); + return HDF_ERR_MALLOC_FAIL; + } + memset(tmp, 0, sizeof(struct HostDevice)); + + ret = HostDeviceInit(tmp); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s:%{public}d HostDeviceInit fail!", __func__, __LINE__); + goto out; + } + + tmp->initFlag = false; + *port = tmp; + return HDF_SUCCESS; +out: + OsalMemFree(tmp); + return ret; +} + +int32_t UsbdRealseDevices(struct UsbdService *service) +{ + if(!service){ + return HDF_ERR_INVALID_PARAM; + } + OsalMutexLock(&service->lock); + while(!HdfSListIsEmpty(&service->devList)){ + struct HostDevice *port = (struct HostDevice *)HdfSListPop(&service->devList); + if(port){ + UsbdRelease(port); + OsalMemFree(port); + } + } + OsalMutexUnlock(&service->lock); + return HDF_SUCCESS; +} + +static struct HostDevice * FindDevFromService(struct UsbdService *service, uint8_t busNum, uint8_t devAddr) +{ + struct HdfSListIterator it; + struct HostDevice *port = NULL; + bool flag = false; + if(!service){ + return NULL; + } + + OsalMutexLock(&service->lock); + HdfSListIteratorInit(&it, &service->devList); + while (HdfSListIteratorHasNext(&it)) { + port = (struct HostDevice *)HdfSListIteratorNext(&it); + if(!port){ + continue; + } + if((port->busNum == busNum) && (port->devAddr == devAddr)){ + flag = true; + break; + } + } + OsalMutexUnlock(&service->lock); + if(!flag){ + return NULL; + } + return port; +} + +static int32_t RemoveDevFromService(struct UsbdService *service, struct HostDevice *port) +{ + struct HdfSListIterator it; + struct HostDevice *tport = NULL; + if(!service || !port){ + return HDF_SUCCESS; + } + + OsalMutexLock(&service->lock); + HdfSListIteratorInit(&it, &service->devList); + while (HdfSListIteratorHasNext(&it)) { + tport = (struct HostDevice *)HdfSListIteratorNext(&it); + if(!tport){ + continue; + } + if((tport->busNum == port->busNum) && (tport->devAddr == port->devAddr)){ + HdfSListIteratorRemove(&it); + break; + } + } + OsalMutexUnlock(&service->lock); + + return HDF_SUCCESS; +} + + + + + diff --git a/hdi/service/src/usbd_function.c b/hdi/service/src/usbd_function.c new file mode 100644 index 00000000..e80bfc60 --- /dev/null +++ b/hdi/service/src/usbd_function.c @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "devmgr_hdi.h" +#include "hdf_log.h" +#include "hdf_remote_service.h" +#include "osal_time.h" +#include "securec.h" +#include "sys_param.h" +#include +#include +#include +#include +#include +#include + +#define DEV_SERVICE_NAME "usbfn_master" +#define DEV_MODULE_NAME "libusbfn_master.z.so" + +#define ACM_SERVICE_NAME "usbfn_cdcacm" +#define ACM_MODULE_NAME "libusbfn_cdcacm.z.so" + +#define ECM_SERVICE_NAME "usbfn_cdcecm" +#define ECM_MODULE_NAME "libusbfn_cdcecm.z.so" + +#define SYS_USB_FFS_READY "sys.usb.ffs.ready" +#define SYS_USB_CONFIGFS "sys.usb.configfs" +#define SYS_USB_CONFIG "sys.usb.config" +#define HDC_CONFIG_OFF "none" +#define HDC_CONFIG_ON "hdc" +#define HDC_CONFIGFS_OFF "0" +#define HDC_CONFIGFS_ON "1" + + +#define FUNCTION_ADD 1 +#define FUNCTION_DEL 2 + +#define START_ACM 100 +#define OPEN_ACM 0 +#define START_ECM 100 + +#define USB_FUNCTION_ACM 1 +#define USB_FUNCTION_ECM 2 +#define USB_FUNCTION_ACM_ECM 3 +#define USB_FUNCTION_HDC 4 + +static uint8_t currentFuncs = 0; + +static struct HdfSBuf *g_data; +static struct HdfSBuf *g_reply; +static struct HdfRemoteService *g_acmService; +static struct HdfRemoteService *g_ecmService; + +static struct HdfRemoteService *usbfnMaster; + +static void RecycleAll() { + if (g_data) { + HdfSBufRecycle(g_data); + g_data = NULL; + } + if (g_reply) { + HdfSBufRecycle(g_reply); + g_reply = NULL; + } + if (g_acmService) { + HdfRemoteServiceRecycle(g_acmService); + g_acmService = NULL; + } + if (g_ecmService) { + HdfRemoteServiceRecycle(g_ecmService); + g_ecmService = NULL; + } +} + +static int CreateOrRemoveUsbfnDevice(int cmd, unsigned char funcMask, int8_t *result) { + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + int status = 0; + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + if (servmgr == NULL) { + HDF_LOGE("%{public}s: HDIServiceManagerGet err", __func__); + return HDF_FAILURE; + } + usbfnMaster = servmgr->GetService(servmgr, DEV_SERVICE_NAME); + HDIServiceManagerRelease(servmgr); + if (usbfnMaster == NULL) { + HDF_LOGE("%{public}s: usbfnMaster err", __func__); + goto err; + } + if (data == NULL || reply == NULL) { + HDF_LOGE("%{public}s: data err", __func__); + goto err; + } + if (!HdfSbufWriteInt8(data, funcMask)) { + HDF_LOGE("%{public}s: HdfSbufWriteInt8 error", __func__); + goto err; + } + status = usbfnMaster->dispatcher->Dispatch(usbfnMaster, cmd, data, reply); + if (status) { + HDF_LOGE("%{public}s: usbfn Dispatch usb create err cmd=%{public}d,status=%{public}d,funcMask=%{public}d", __func__, cmd, status, funcMask); + goto err; + } + if (!HdfSbufReadInt8(data, result)) { + HDF_LOGE("%{public}s: HdfSbufReadInt8 result error", __func__); + goto err; + } + if (usbfnMaster) { + HdfRemoteServiceRecycle(usbfnMaster); + usbfnMaster = NULL; + } + if (data) { + HdfSBufRecycle(data); + data = NULL; + } + if (reply) { + HdfSBufRecycle(reply); + reply = NULL; + } + return HDF_SUCCESS; + err: + if (usbfnMaster) { + HdfRemoteServiceRecycle(usbfnMaster); + usbfnMaster = NULL; + } + if (data) { + HdfSBufRecycle(data); + data = NULL; + } + if (reply) { + HdfSBufRecycle(reply); + reply = NULL; + } + return HDF_FAILURE; +} + +//1. ж(acm/ecm, hdc) 2.ټ +int UsbdSetFunction(int func) { + HDF_LOGI("%{public}s:%{public}d UsbdSetFunction start func = %{public}d \n", __func__, __LINE__, func); + int status = 0; + uint8_t _acm_ecm = func & USB_FUNCTION_ACM_ECM; + int8_t result = 0; + + if (func < 0 || func > 6) { + HDF_LOGE("%{public}s:%{public}d func invalid \n", __func__, __LINE__); + return HDF_FAILURE; + } + //1. hdc ж + if(func & USB_FUNCTION_HDC){ + status = SystemSetParameter(SYS_USB_CONFIGFS, HDC_CONFIGFS_ON); + if (status) { + HDF_LOGE("%{public}s: add hdc configfs err = %{public}d", __func__, status); + return HDF_FAILURE; + } + status = SystemSetParameter(SYS_USB_CONFIG, HDC_CONFIG_ON); + if (status) { + HDF_LOGE("%{public}s: add hdc config err = %{public}d", __func__, status); + return HDF_FAILURE; + } + } else { + status = SystemSetParameter(SYS_USB_CONFIG, HDC_CONFIG_OFF); + if(status){ + HDF_LOGE("%{public}s: SystemSetParameter SYS_USB_CONFIG OFF err = %{public}d", __func__, status); + return HDF_FAILURE; + } + status = SystemSetParameter(SYS_USB_CONFIGFS, HDC_CONFIGFS_OFF); + if(status){ + HDF_LOGE("%{public}s: SystemSetParameter SYS_USB_CONFIGFS OFF err = %{public}d", __func__, status); + return HDF_FAILURE; + } + } + //1. ж(acm/ecm), жϷֵ + status = CreateOrRemoveUsbfnDevice(FUNCTION_DEL, 0, &result); + if (status) { + HDF_LOGE("%{public}s: usbfn remove function err = %{public}d", __func__, status); + } + HDF_LOGE("%{public}s: usbfn remove function result = %{public}d", __func__, result); + if(result){ + return HDF_FAILURE; + } + //2.豸acm/ecm + if(_acm_ecm > 0 && _acm_ecm < 4){ + HDF_LOGE("%{public}s: usbfn _acm_ecm = %{public}d", __func__, _acm_ecm); + status = CreateOrRemoveUsbfnDevice(FUNCTION_ADD, _acm_ecm, &result); + if (status) { + HDF_LOGE("%{public}s: usbfn add function err = %{public}d result=%{public}d", __func__, status ,result); + return HDF_FAILURE; + } + if (result != _acm_ecm) { + HDF_LOGE("%{public}s:%{public}d add function result=%{public}d", __func__, __LINE__,result); + return HDF_FAILURE; + } + } + //2.(acm/ecm) + g_data = HdfSBufTypedObtain(SBUF_IPC); + g_reply = HdfSBufTypedObtain(SBUF_IPC); + if (func & USB_FUNCTION_ACM) { + HDF_LOGE("%{public}s: USB_FUNCTION_ACM = %{public}d", __func__, func); + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + if (servmgr == NULL) { + HDF_LOGE("%{public}s: usbfn HDIServiceManagerGet err", __func__); + goto err; + } + g_acmService = servmgr->GetService(servmgr, ACM_SERVICE_NAME); + HDIServiceManagerRelease(servmgr); + if (g_acmService) { + HDF_LOGE("%{public}s: g_acmService Dispatch start", __func__); + status = g_acmService->dispatcher->Dispatch(g_acmService, START_ACM, g_data, g_reply); + if (status) { + HDF_LOGE("%{public}s: g_acmService START_ACM err = %{public}d", __func__, status); + goto err; + } + status = g_acmService->dispatcher->Dispatch(g_acmService, OPEN_ACM, g_data, g_reply); + if (status) { + HDF_LOGE("%{public}s: g_acmService OPEN_ACM err = %{public}d", __func__, status); + goto err; + } + } else { + goto err; + } + } + if (func & USB_FUNCTION_ECM) { + HDF_LOGE("%{public}s: USB_FUNCTION_ECM = %{public}d", __func__, func); + struct HDIServiceManager *servmgr = HDIServiceManagerGet(); + if (servmgr == NULL) { + HDF_LOGE("%{public}s: usbfn HDIServiceManagerGet err", __func__); + goto err; + } + HDF_LOGE("%{public}s: USB_FUNCTION_ECM 2 = %{public}d", __func__, func); + g_ecmService = servmgr->GetService(servmgr, ECM_SERVICE_NAME); + HDF_LOGE("%{public}s: USB_FUNCTION_ECM 3 = %{public}d", __func__, func); + HDIServiceManagerRelease(servmgr); + HDF_LOGE("%{public}s: USB_FUNCTION_ECM 4 = %{public}d", __func__, func); + if (g_ecmService) { + HDF_LOGE("%{public}s: g_ecmService Dispatch start", __func__); + status = g_ecmService->dispatcher->Dispatch(g_ecmService, START_ECM, g_data, g_reply); + if (status) { + HDF_LOGE("%{public}s: g_ecmService START_ECM err = %{public}d", __func__, status); + goto err; + } + } else { + goto err; + } + } + currentFuncs = func; + RecycleAll(); + HDF_LOGI("%{public}s:%{public}d UsbdSetFunction end \n", __func__, __LINE__); + return HDF_SUCCESS; + err: + currentFuncs = 0; + RecycleAll(); + return HDF_FAILURE; +} + +int32_t UsbdGetFunction() { + HDF_LOGI("%{public}s:%{public}d UsbdGetFunction:%{public}d\n", __func__, __LINE__, currentFuncs); + return currentFuncs; +} diff --git a/hdi/service/src/usbd_port.c b/hdi/service/src/usbd_port.c new file mode 100644 index 00000000..6d915858 --- /dev/null +++ b/hdi/service/src/usbd_port.c @@ -0,0 +1,144 @@ +#include "usbd_port.h" +#include "usbd_publisher.h" +#include "usbd_function.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "hdf_log.h" +#include "osal_time.h" +#include "securec.h" +#include +#include +#include +#include +#include +#include "sys_param.h" + +//mode +//ufp: host power:source data:host +//dfp:device power:sink data:device +//drp: ufp&dfp + +#define PORT_MODE_NONE_STR "none" +#define PORT_MODE_HOST_STR "host" +#define PORT_MODE_DEVICE_STR "device" +#define PORT_MODE_OTG_STR "otg" + +#define DEFAULT_PORT_ID 1 + +#define POWER_ROLE_NONE 0 +#define POWER_ROLE_SOURCE 1 +#define POWER_ROLE_SINK 2 + +#define DATA_ROLE_NONE 0 +#define DATA_ROLE_HOST 1 +#define DATA_ROLE_DEVICE 2 + +#define PORT_MODE_NONE 0 +#define PORT_MODE_HOST 1 +#define PORT_MODE_DEVICE 2 + +#define SYS_USB_PORT "sys.usb.port" +#define SYS_USB_FFS_READY "sys.usb.ffs.ready" +#define SYS_USB_CONFIGFS "sys.usb.configfs" +#define SYS_USB_CONFIG "sys.usb.config" +#define HDC_CONFIG_OFF "none" +#define HDC_CONFIG_ON "hdc" + +static int32_t currentPortId = 1; +static int32_t currentPowerRole = 1; +static int32_t currentDataRole = 1; +static int32_t currentMode = 1; + +int SetPort(int portId , int powerRole, int dataRole, struct UsbdService *service) { + HDF_LOGE("%{public}s: portId:%{public}d,powerRole:%{public}d,dataRole:%{public}d start", __func__, portId, powerRole, dataRole); + int ret = 0; + char *fn = "/sys/kernel/debug/usb/100e0000.hidwc3_0/mode"; + int fd = -1; + int len = 0; + int mode = 0; + char *mode_str = NULL; + if (portId != DEFAULT_PORT_ID) + { + return -1; + } + if (powerRole < 0 || powerRole >= 3) { + return -1; + } + if (dataRole < 0 || dataRole >= 3) { + return -1; + } + if (powerRole == POWER_ROLE_SOURCE && dataRole == DATA_ROLE_HOST){ + mode = PORT_MODE_HOST; + } + if (powerRole == POWER_ROLE_SINK && dataRole == DATA_ROLE_DEVICE){ + mode = PORT_MODE_DEVICE; + } + if (mode == PORT_MODE_HOST || mode == PORT_MODE_DEVICE) { + switch (mode) { + case PORT_MODE_HOST: + mode_str = PORT_MODE_HOST_STR; + break; + case PORT_MODE_DEVICE: + mode_str = PORT_MODE_DEVICE_STR; + break; + default: + mode_str = PORT_MODE_NONE; + break; + } + } + HDF_LOGE("%{public}s: mode = %{public}d, start", __func__, mode); + if (!mode_str) { + return -1; + } + len = strlen(mode_str); + fd = open(fn, O_WRONLY | O_TRUNC); + if (fd < 0) { + HDF_LOGE("%{public}s: file open error fd= %{public}d", __func__, fd); + return -1; + } + // otg host device + ret = write(fd, mode_str, len); + HDF_LOGE("%{public}s: set mode=%{public}d result:%{public}d len:%{pulibic}d", __func__, mode,ret, len); + if (ret == len) { + ret = 0; + currentPortId = portId; + currentPowerRole = powerRole; + currentDataRole = dataRole; + currentMode = mode; + NotifyUsbPortSubscriber(service->subscriber, portId, powerRole, dataRole); + // HDF_LOGE("%{public}s: SystemSetParameter start", __func__); + // SystemSetParameter(SYS_USB_CONFIG, HDC_CONFIG_ON); + // HDF_LOGE("%{public}s: SystemSetParameter end", __func__); + } else { + ret = -1; + } + close(fd); + HDF_LOGE("%{public}s: set mode end", __func__); + return 0; +} +static int SetDefaultPort(struct UsbdService *service){ + //默认 device模式 + return SetPort(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE, service); +} + +int QueryPort(int *portId , int *powerRole, int *dataRole, int *mode, struct UsbdService *service){ + int ret = SetDefaultPort(service); + if (ret) + { + return -1; + } + *portId = currentPortId; + *powerRole = currentPowerRole; + *dataRole = currentDataRole; + *mode = currentMode; + UsbdSetFunction(0); + UsbdSetFunction(4); + return 0; +} diff --git a/hdi/service/src/usbd_publisher.c b/hdi/service/src/usbd_publisher.c new file mode 100644 index 00000000..4775d446 --- /dev/null +++ b/hdi/service/src/usbd_publisher.c @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usbd_publisher.h" + +#include "hdf_remote_service.h" +#include "utils/hdf_log.h" + +#define HDF_LOG_TAG usbd_publisher + + +void NotifySubscriberDevice(const struct UsbdSubscriber *subscriber, enum UsbdDeviceAction act, int32_t busNum, int32_t devNum) +{ + HDF_LOGD("%{public}s: enter subscriber:%{public}s act:%{public}d bus:%{public}d dev:%{public}d", __func__, + subscriber ? "OK":"NULL", act, busNum, devNum); + if (subscriber == NULL) { + HDF_LOGD("%{public}s: subscriber is NULL", __func__); + return; + } + int ret; + struct HdfRemoteService *service = subscriber->remoteService; + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + HDF_LOGD("%{public}s: enter subscriber:%{pubblic}s act:%{public}d bus:%{public}d dev:%{public}d subscriber:%{public}p service:%{public}p", __func__, + subscriber ? "OK":"NULL", act, busNum, devNum, subscriber, service); + if (data == NULL || reply == NULL) { + HDF_LOGE("%{public}s failed to obtain hdf sbuf", __func__); + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return; + } + HdfSbufWriteInt32(data, act); + HdfSbufWriteInt32(data, busNum); + HdfSbufWriteInt32(data, devNum); + ret = service->dispatcher->Dispatch(service, CMD_NOTIFY_SUBSCRIBER_DEVICE_EVENT, data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed to notify subscriber, ret: %{public}d", __func__, ret); + } else { + HDF_LOGD("%{public}s: succeed to notify subscriber", __func__); + } + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} + +void NotifyUsbPortSubscriber(const struct UsbdSubscriber *subscriber, int32_t portId, int32_t powerRole, int32_t dataRole){ + HDF_LOGD("%{public}s: enter", __func__); + if (subscriber == NULL) { + HDF_LOGD("%{public}s: subscriber is NULL", __func__); + return; + } + int ret; + struct HdfRemoteService *service = subscriber->remoteService; + struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC); + struct HdfSBuf *reply = HdfSBufTypedObtain(SBUF_IPC); + if (data == NULL || reply == NULL) { + HDF_LOGE("%{public}s failed to obtain hdf sbuf", __func__); + HdfSBufRecycle(data); + HdfSBufRecycle(reply); + return; + } + HdfSbufWriteInt32(data, portId); + HdfSbufWriteInt32(data, powerRole); + HdfSbufWriteInt32(data, dataRole); + + ret = service->dispatcher->Dispatch(service, CMD_NOTIFY_PORT_CHANGED, data, reply); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s failed to notify subscriber, ret: %{public}d", __func__, ret); + } else { + HDF_LOGD("%{public}s: succeed to notify subscriber", __func__); + } + HdfSBufRecycle(data); + HdfSBufRecycle(reply); +} \ No newline at end of file diff --git a/hdi/test/BUILD.gn b/hdi/test/BUILD.gn new file mode 100644 index 00000000..e3c24a7b --- /dev/null +++ b/hdi/test/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2021 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. + +import("//build/test.gni") + +#################################group######################################### +group("usbd_client_unittest") { + testonly = true + deps = [ "unittest/common:unittest", ] + +} +############################################################################### \ No newline at end of file diff --git a/hdi/test/unittest/common/BUILD.gn b/hdi/test/unittest/common/BUILD.gn new file mode 100644 index 00000000..3cedf1b2 --- /dev/null +++ b/hdi/test/unittest/common/BUILD.gn @@ -0,0 +1,293 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") +import("//build/test.gni") + +module_output_path = "usb_manager_native/usbd_client_test" + +############################################################################### +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "include", + "//utils/system/safwk/native/include", + "//base/usb/usb_manager/hdi/client/include", + ] +} + +##############################unittest##########################################= + +ohos_unittest("test_config") { + module_out_path = module_output_path + sources = [ + "src/usbd_config_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_transfer") { + module_out_path = module_output_path + sources = [ + "src/usbd_transfer_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_descriptor") { + module_out_path = module_output_path + sources = [ + "src/usbd_descriptor_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_device") { + module_out_path = module_output_path + sources = [ + "src/usbd_device_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_function") { + module_out_path = module_output_path + sources = [ + "src/usbd_function_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_interface") { + module_out_path = module_output_path + sources = [ + "src/usbd_interface_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_port") { + module_out_path = module_output_path + sources = [ + "src/usbd_port_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_request") { + module_out_path = module_output_path + sources = [ + "src/usbd_request_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +############################################################################### +group("unittest") { + testonly = true + deps = [ + ":test_config", + ":test_descriptor", + ":test_device", + ":test_function", + ":test_interface", + ":test_port", + ":test_request", + ] +} +############################################################################### \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_config_test.h b/hdi/test/unittest/common/include/usbd_config_test.h new file mode 100644 index 00000000..08e8a996 --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_config_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_CONFIG_TEST_H +#define USBD_CONFIG_TEST_H + +#include + +class UsbdConfigTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_CONFIG_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_descriptor_test.h b/hdi/test/unittest/common/include/usbd_descriptor_test.h new file mode 100644 index 00000000..55a0e59e --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_descriptor_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_DESCRIPTOR_TEST_H +#define USBD_DESCRIPTOR_TEST_H + +#include + +class UsbdDescriptorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_DESCRIPTOR_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_device_test.h b/hdi/test/unittest/common/include/usbd_device_test.h new file mode 100644 index 00000000..3418d0d8 --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_device_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_DEVICE_TEST_H +#define USBD_DEVICE_TEST_H + +#include + +class UsbdDeviceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_DEVICE_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_function_test.h b/hdi/test/unittest/common/include/usbd_function_test.h new file mode 100644 index 00000000..97e460b8 --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_function_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_FUNCTION_TEST_H +#define USBD_FUNCTION_TEST_H + +#include + +class UsbdFunctionTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_FUNCTION_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_interface_test.h b/hdi/test/unittest/common/include/usbd_interface_test.h new file mode 100644 index 00000000..e0fbd8ff --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_interface_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_INTERFACE_TEST_H +#define USBD_INTERFACE_TEST_H + +#include + +class UsbdInterfaceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_INTERFACE_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_port_test.h b/hdi/test/unittest/common/include/usbd_port_test.h new file mode 100644 index 00000000..9ac29dfc --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_port_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_PORT_TEST_H +#define USBD_PORT_TEST_H + +#include + +class UsbdPortTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_PORT_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_request_test.h b/hdi/test/unittest/common/include/usbd_request_test.h new file mode 100644 index 00000000..f20324c6 --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_request_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_REQUEST_TEST_H +#define USBD_REQUEST_TEST_H + +#include + +class UsbdRequestTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_REQUEST_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/include/usbd_transfer_test.h b/hdi/test/unittest/common/include/usbd_transfer_test.h new file mode 100644 index 00000000..3947520f --- /dev/null +++ b/hdi/test/unittest/common/include/usbd_transfer_test.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021 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. + */ +#ifndef USBD_TRANSFER_TEST_H +#define USBD_TRANSFER_TEST_H + +#include + +class UsbdTransferTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USBD_TRANSFER_TEST_H \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_config_test.cpp b/hdi/test/unittest/common/src/usbd_config_test.cpp new file mode 100644 index 00000000..9a17bbf0 --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_config_test.cpp @@ -0,0 +1,488 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_config_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbdConfigTest::SetUpTestCase(void) +{ +} + +void UsbdConfigTest::TearDownTestCase(void) +{ +} + +void UsbdConfigTest::SetUp(void) +{ +} + +void UsbdConfigTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbdConfig001 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig001,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig001 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig001 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig001 end."); +} + +/** + * @tc.name: UsbdConfig002 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig002,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig002 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig002 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig002 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig002 end."); +} + +/** + * @tc.name: UsbdConfig003 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig003,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig003 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig003 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig003 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig003 end."); +} + +/** + * @tc.name: UsbdConfig004 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig004,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig004 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig004 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 1; + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig004 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig004 end."); +} + +/** + * @tc.name: UsbdConfig005 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig005,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig005 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig005 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig005 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig005 end."); +} + +/** + * @tc.name: UsbdConfig006 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig006,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig006 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig006 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 1; + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig006 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig006 end."); +} + +/** + * @tc.name: UsbdConfig007 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig007,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig007 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig007 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t configIndex = 1; + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig007 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig007 end."); +} + +/** + * @tc.name: UsbdConfig008 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:busNum、devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig008,TestSize.Level1) +{ + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t configIndex = 1; + auto ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig008 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig008 end."); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdConfig011 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig011,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011 end."); +} + +/** + * @tc.name: UsbdConfig012 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig012,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig012 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig012 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig012 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig012 end."); +} + +/** + * @tc.name: UsbdConfig013 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig013,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig013 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig013 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig013 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig013 end."); +} + +/** + * @tc.name: UsbdConfig014 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig014,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 end."); +} + +/** + * @tc.name: UsbdConfig015 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig015,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 end."); +} + +/** + * @tc.name: UsbdConfig016 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig016,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 end."); +} + +/** + * @tc.name: UsbdConfig017 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig017,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 end."); +} + +/** + * @tc.name: UsbdConfig018 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig018,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 end."); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_config_test.cpp.bak b/hdi/test/unittest/common/src/usbd_config_test.cpp.bak new file mode 100644 index 00000000..367515cc --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_config_test.cpp.bak @@ -0,0 +1,493 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_config_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +UsbDevice device; + +USBConfig config; + +void UsbdConfigTest::SetUpTestCase(void) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Start UsbdConfigTest-----------"); + + vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest:: Line:%{public}d Get DeviceList Result = %{public}d & DeviceList Size = %{public}d",__LINE__, ret, deviceList.size()); + ASSERT_TRUE(ret == ERR_OK); + + ASSERT_TRUE(!(deviceList.empty())) << "DeviceList NULL"; + + device = deviceList.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest:: Line:%{public}d Device Info = %{public}s",__LINE__, device.ToString().c_str()); + + config = device.GetConfigs().front(); +} + +void UsbdConfigTest::TearDownTestCase(void) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------End UsbdConfigTest-----------"); +} + +void UsbdConfigTest::SetUp(void) +{ +} + +void UsbdConfigTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbdConfig001 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig001,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig001 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig001-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret == ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig001 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig002 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig002,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig002 : SetConfig-----------"); + + uint8_t busNum = 255; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig002-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig002 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig003 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig003,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig003 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 255; + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig003-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig003 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig004 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig004,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig004 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 255; + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig004-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig004 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig005 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig005,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig005 : SetConfig-----------"); + + uint8_t busNum = 255; + uint8_t devAddr = 255; + uint8_t configIndex = config.GetId(); + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig005-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig005 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig006 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig006,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig006 : SetConfig-----------"); + + uint8_t busNum = 255; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 255; + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig006-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig006 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig007 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig007,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig007 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 255; + uint8_t configIndex = 255; + ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig007-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig007 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig008 + * @tc.desc: Test functions to SetConfig + * @tc.desc: int32_t SetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:busNum、devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig008,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig008 : SetConfig-----------"); + + uint8_t busNum = 255; + uint8_t devAddr = 255; + uint8_t configIndex = 255; + auto ret = UsbdClient::SetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig008-> Line:%{public}d SetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig007 : SetConfig-----------"); +} +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdConfig011 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig011,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig011 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret == ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig011 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig012 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig012,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig012 : SetConfig-----------"); + + uint8_t busNum = 255; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig012-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig012 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig013 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig013,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig013 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 255; + uint8_t configIndex = config.GetId(); + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig013-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig013 : SetConfig-----------"); +} + +/** + * @tc.name: UsbdConfig014 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig014,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig014 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 255; + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret != ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig014 : SetConfig-----------"); + + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig014 end."); +} + +/** + * @tc.name: UsbdConfig015 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig015,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig011 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret == ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig011 : SetConfig-----------"); + + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + std::vector configs = device.GetConfigs(); + USBConfig config = configs.front(); + uint8_t configIndex = config.GetId(); + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig015 end."); +} + +/** + * @tc.name: UsbdConfig016 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig016,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig011 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret == ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig011 : SetConfig-----------"); + + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig016 end."); +} + +/** + * @tc.name: UsbdConfig017 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig017,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig011 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret == ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig011 : SetConfig-----------"); + + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig017 end."); +} + +/** + * @tc.name: UsbdConfig018 + * @tc.desc: Test functions to GetConfig + * @tc.desc: int32_t GetConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) + * @tc.desc: 反向测试:参数异常,busNum、devAddr、configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdConfigTest,UsbdConfig018,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdConfig011 : SetConfig-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configIndex = config.GetId(); + auto ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig011-> Line:%{public}d GetConfig Result = %{public}d", __LINE__, ret); + ASSERT_TRUE(ret == ERR_OK); + + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdConfig011 : SetConfig-----------"); + + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t configIndex = 1; + ret = UsbdClient::GetConfig(busNum, devAddr, configIndex); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdConfig018 end."); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_descriptor_test.cpp b/hdi/test/unittest/common/src/usbd_descriptor_test.cpp new file mode 100644 index 00000000..58d032b7 --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_descriptor_test.cpp @@ -0,0 +1,928 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_descriptor_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbdDescriptorTest::SetUpTestCase(void) +{ +} + +void UsbdDescriptorTest::TearDownTestCase(void) +{ +} + +void UsbdDescriptorTest::SetUp(void) +{ +} + +void UsbdDescriptorTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbdDescriptor001 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor001,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor001 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[1] = {1}; + uint32_t length = 1; + ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor001 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor001 end."); +} + +/** + * @tc.name: UsbdDescriptor002 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor002,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor002 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor002 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor002 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor002 end."); +} + +/** + * @tc.name: UsbdDescriptor003 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor003,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor003 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor003 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor003 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor003 end."); +} + +/** + * @tc.name: UsbdDescriptor004 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor004,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor004 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor004 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor004 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor004 end."); +} + +/** + * @tc.name: UsbdDescriptor005 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor005,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor005 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor005 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor005 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor005 end."); +} + +/** + * @tc.name: UsbdDescriptor006 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor006,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor006 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor006 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor006 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor006 end."); +} + +/** + * @tc.name: UsbdDescriptor007 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor007,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor007 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor007 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor007 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor007 end."); +} + +/** + * @tc.name: UsbdDescriptor008 + * @tc.desc: Test functions to GetRawDescriptor + * @tc.desc: int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:busNum、devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor008,TestSize.Level1) +{ + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + auto ret = UsbdClient::GetRawDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor008 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor008 end."); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdDescriptor011 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor011,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor011 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor011 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor011 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor011 end."); +} + +/** + * @tc.name: UsbdDescriptor012 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor012,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor012 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor012 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor012 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor012 end."); +} + +/** + * @tc.name: UsbdDescriptor013 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor013,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor013 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor013 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor013 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor013 end."); +} + +/** + * @tc.name: UsbdDescriptor014 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor014,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor014 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor014 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor014 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor014 end."); +} + +/** + * @tc.name: UsbdDescriptor015 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor015,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor015 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor015 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor015 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor015 end."); +} + +/** + * @tc.name: UsbdDescriptor016 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor016,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor016 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor016 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor016 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor016 end."); +} + +/** + * @tc.name: UsbdDescriptor017 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor017,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor017 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor017 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor017 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor017 end."); +} + +/** + * @tc.name: UsbdDescriptor018 + * @tc.desc: Test functions to GetDeviceDescriptor + * @tc.desc: int32_t GetDeviceDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:busNum、devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor018,TestSize.Level1) +{ + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + auto ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor018 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor018 end."); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdDescriptor021 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor021,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor021 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor021 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor021 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor021 end."); +} + +/** + * @tc.name: UsbdDescriptor022 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor022,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor022 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor022 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor022 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor022 end."); +} + +/** + * @tc.name: UsbdDescriptor023 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor023,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor023 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor023 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor023 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor023 end."); +} + +/** + * @tc.name: UsbdDescriptor024 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor024,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor024 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor024 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor024 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor024 end."); +} + +/** + * @tc.name: UsbdDescriptor025 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor025,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor025 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor025 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor025 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor025 end."); +} + +/** + * @tc.name: UsbdDescriptor026 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor026,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor026 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor026 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor026 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor026 end."); +} + +/** + * @tc.name: UsbdDescriptor027 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor027,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor027 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor027 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor027 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor027 end."); +} + +/** + * @tc.name: UsbdDescriptor028 + * @tc.desc: Test functions to GetStringDescriptor + * @tc.desc: int32_t GetStringDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t stringId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:busNum、devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor028,TestSize.Level1) +{ + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t stringId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + auto ret = UsbdClient::GetStringDescriptor(busNum, devAddr, stringId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor028 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor028 end."); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdDescriptor031 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor031,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor031 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor031 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor031 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor031 end."); +} + +/** + * @tc.name: UsbdDescriptor032 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor032,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor032 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor032 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor032 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor032 end."); +} + +/** + * @tc.name: UsbdDescriptor033 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor033,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor033 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor033 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor033 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor033 end."); +} + +/** + * @tc.name: UsbdDescriptor034 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor034,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor034 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor034 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor034 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor034 end."); +} + +/** + * @tc.name: UsbdDescriptor035 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor035,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor035 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor035 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = 1; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor035 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor035 end."); +} + +/** + * @tc.name: UsbdDescriptor036 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:参数异常,busNum、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor036,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor036 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor036 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = 1; + uint8_t devAddr = device.GetDevAddr(); + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor036 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor036 end."); +} + +/** + * @tc.name: UsbdDescriptor037 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor037,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor037 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor037 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor037 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor037 end."); +} + +/** + * @tc.name: UsbdDescriptor038 + * @tc.desc: Test functions to GetConfigDescriptor + * @tc.desc: int32_t GetConfigDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t configId, uint8_t *buffer, uint32_t &length) + * @tc.desc: 反向测试:busNum、devAddr、length错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDescriptorTest,UsbdDescriptor038,TestSize.Level1) +{ + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t configId = 1; + uint8_t buffer[] = {0}; + uint32_t length = -1; + auto ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, configId, buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor038 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdDescriptorTest::UsbdDescriptor038 end."); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_device_test.cpp b/hdi/test/unittest/common/src/usbd_device_test.cpp new file mode 100644 index 00000000..1e3ea7f3 --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_device_test.cpp @@ -0,0 +1,237 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_device_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +vector deviceList; + +UsbDevice device; + +auto& usbSrvClient = UsbSrvClient::GetInstance(); + +void UsbdDeviceTest::SetUpTestCase(void) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Start UsbdDeviceTest-----------"); + auto ret = usbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d Get DeviceList OK, result = %{public}d",__LINE__, ret); + ASSERT_TRUE(!(deviceList.empty())) << "DeviceList NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d DeviceList size=%{public}d", __LINE__, deviceList.size()); + device = deviceList.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d DeviceList device.ToString=%{public}s", __LINE__, device.ToString().c_str()); +} + +void UsbdDeviceTest::TearDownTestCase(void) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------End UsbdDeviceTest-----------"); +} + +void UsbdDeviceTest::SetUp(void) +{ +} + +void UsbdDeviceTest::TearDown(void) +{ +} + + + +/** + * @tc.name: UsbdDevice001 + * @tc.desc: Test functions to OpenDevice + * @tc.desc: int32_t OpenDevice(busNum ,devAddr) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice001,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice001 : OpenDevice-----------"); + // vector deviceList; + // UsbDevice device; + // auto& usbSrvClient = UsbSrvClient::GetInstance(); + // auto ret = usbSrvClient.GetDevices(deviceList); + // ASSERT_TRUE(ret == ERR_OK); + // USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d Get DeviceList OK, result = %{public}d",__LINE__, ret); + // ASSERT_TRUE(!(deviceList.empty())) << "DeviceList NULL"; + // USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d DeviceList size=%{public}d", __LINE__, deviceList.size()); + // device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d OpenDevice GetBusNum =%{public}d GetDevAddr =%{public}d", __LINE__, device.GetBusNum(),device.GetDevAddr()); + //int32_t fd = 0; + auto ret = UsbdClient::OpenDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d OpenDevice result =%{public}d", __LINE__, ret); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice001 : OpenDevice-----------"); +} + +/** + * @tc.name: UsbdDevice002 + * @tc.desc: Test functions to OpenDevice + * @tc.desc: int32_t OpenDevice(busNum ,devAddr) + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice002,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice002 : OpenDevice-----------"); + uint8_t busNum = 255; + uint8_t devAddr = device.GetDevAddr(); + //int32_t fd = 0; + auto ret = UsbdClient::OpenDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d OpenDevice result=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice002 : OpenDevice-----------"); +} + +/** + * @tc.name: UsbdDevice003 + * @tc.desc: Test functions to OpenDevice + * @tc.desc: int32_t OpenDevice(busNum ,devAddr) + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice003,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice003 : OpenDevice-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 255; + //int32_t fd = 0; + auto ret = UsbdClient::OpenDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d OpenDevice result=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice003 : OpenDevice-----------"); +} + +/** + * @tc.name: UsbdDevice004 + * @tc.desc: Test functions to OpenDevice + * @tc.desc: int32_t OpenDevice(busNum ,devAddr) + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice004,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice004 : OpenDevice-----------"); + uint8_t busNum = 255; + uint8_t devAddr = 255; + //int32_t fd = 0; + auto ret = UsbdClient::OpenDevice(busNum ,devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d OpenDevice result=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice004 : OpenDevice-----------"); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdDevice011 + * @tc.desc: Test functions to CloseDevice + * @tc.desc: int32_t CloseDevice(uint8_t busNum, uint8_t devAddr) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice011,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice011 : Close-----------"); + USBDevicePipe pipe; + // auto& usbSrvClient = UsbSrvClient::GetInstance(); + usbSrvClient.OpenDevice(device,pipe); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + auto ret = UsbdClient::CloseDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d Close result=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice011 : Close-----------"); +} + +/** + * @tc.name: UsbdDevice012 + * @tc.desc: Test functions to CloseDevice + * @tc.desc: int32_t CloseDevice(uint8_t busNum, uint8_t devAddr) + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice012,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice012 : Close-----------"); + USBDevicePipe pipe; + // auto& usbSrvClient = UsbSrvClient::GetInstance(); + usbSrvClient.OpenDevice(device,pipe); + uint8_t busNum = 255; + uint8_t devAddr = device.GetDevAddr(); + auto ret = UsbdClient::CloseDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d Close result=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice012 : Close-----------"); +} + +/** + * @tc.name: UsbdDevice013 + * @tc.desc: Test functions to CloseDevice + * @tc.desc: int32_t CloseDevice(uint8_t busNum, uint8_t devAddr) + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice013,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice013 : Close-----------"); + USBDevicePipe pipe; + // auto& usbSrvClient = UsbSrvClient::GetInstance(); + usbSrvClient.OpenDevice(device,pipe); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 255; + auto ret = UsbdClient::CloseDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d Close result=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice013 : Close-----------"); +} + +/** + * @tc.name: UsbdDevice014 + * @tc.desc: Test functions to CloseDevice + * @tc.desc: int32_t CloseDevice(uint8_t busNum, uint8_t devAddr) + * @tc.desc: + * @tc.type: FUNC + */ +HWTEST_F(UsbdDeviceTest,UsbdDevice014,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdDevice014 : Close-----------"); + USBDevicePipe pipe; + // auto& usbSrvClient = UsbSrvClient::GetInstance(); + usbSrvClient.OpenDevice(device,pipe); + uint8_t busNum = 255; + uint8_t devAddr = 255; + auto ret = UsbdClient::CloseDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d Close result=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdDevice014 : Close-----------"); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_function_test.cpp b/hdi/test/unittest/common/src/usbd_function_test.cpp new file mode 100644 index 00000000..ac8b030d --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_function_test.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_function_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbdFunctionTest::SetUpTestCase(void) +{ +} + +void UsbdFunctionTest::TearDownTestCase(void) +{ +} + +void UsbdFunctionTest::SetUp(void) +{ +} + +void UsbdFunctionTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbdFunction001 + * @tc.desc: Test functions to GetCurrentFunctions + * @tc.desc: int32_t GetCurrentFunctions() + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdFunctionTest,UsbdFunction001,TestSize.Level1) +{ + auto ret = UsbdClient::GetCurrentFunctions(); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdFunctionTest::UsbdFunction001 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdFunctionTest::UsbdFunction001 end."); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdFunction011 + * @tc.desc: Test functions to SetCurrentFunctions + * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdFunctionTest,UsbdFunction011,TestSize.Level1) +{ + auto funcs = UsbdClient::GetCurrentFunctions(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdFunctionTest::UsbdFunction011 %{public}d funcs=%{public}d",__LINE__ ,funcs); + auto ret = UsbdClient::SetCurrentFunctions(funcs); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdFunctionTest::UsbdFunction011 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdFunctionTest::UsbdFunction011 end."); +} + +/** + * @tc.name: UsbdFunction012 + * @tc.desc: Test functions to SetCurrentFunctions + * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs) + * @tc.desc: 反向测试:参数异常,funcs错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdFunctionTest,UsbdFunction012,TestSize.Level1) +{ + int32_t funcs = -1; + auto ret = UsbdClient::SetCurrentFunctions(funcs); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdFunctionTest::UsbdFunction011 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdFunctionTest::UsbdFunction011 end."); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_interface_test.cpp b/hdi/test/unittest/common/src/usbd_interface_test.cpp new file mode 100644 index 00000000..20d6ffd4 --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_interface_test.cpp @@ -0,0 +1,576 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_interface_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +std::vector deviceList; +auto& usbSrvClient = UsbSrvClient::GetInstance(); +UsbDevice device; +UsbInterface interface; + +void UsbdInterfaceTest::SetUpTestCase(void) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Start UsbdDeviceTest-----------"); + auto ret = usbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface001 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + device = deviceList.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdDeviceTest:: Line:%{public}d DeviceList device.ToString=%{public}s", __LINE__, device.ToString().c_str()); + ret = usbSrvClient.OpenDevice(device,pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(1); +} + +void UsbdInterfaceTest::TearDownTestCase(void) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------End UsbdDeviceTest-----------"); +} + +void UsbdInterfaceTest::SetUp(void) +{ +} + +void UsbdInterfaceTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbdInterface001 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface001,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface001 : ClaimInterface-----------"); + + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = interface.GetId(); + auto ret = UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface001 %{public}d ReleaseInterface=%{public}d",__LINE__ ,ret); + ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface001 %{public}d ClaimInterface=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface001 : ClaimInterface-----------"); +} + +/** + * @tc.name: UsbdInterface002 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface002,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface002 : ClaimInterface-----------"); + uint8_t busNum = 20; + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = interface.GetId(); + auto ret = UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface002 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface002 : ClaimInterface-----------"); +} + +/** + * @tc.name: UsbdInterface003 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface003,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface003 : ClaimInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 1; + uint8_t interfaceid = interface.GetId(); + UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + auto ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface003 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface003 : ClaimInterface-----------"); +} + +/** + * @tc.name: UsbdInterface004 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface004,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface004 : ClaimInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = 20; + UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + auto ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface004 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface004 : ClaimInterface-----------"); +} + +/** + * @tc.name: UsbdInterface005 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface005,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface005 : ClaimInterface-----------"); + uint8_t busNum = 1; + uint8_t devAddr = 1; + uint8_t interfaceid = interface.GetId(); + UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + auto ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface005 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface005 : ClaimInterface-----------"); +} + +/** + * @tc.name: UsbdInterface006 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,busNum、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface006,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface006 : ClaimInterface-----------"); + uint8_t busNum = 25; + uint8_t devAddr = device.GetDevAddr(); + int32_t interfaceid = 255; + UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + auto ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface006 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface006 : ClaimInterface-----------"); +} + +/** + * @tc.name: UsbdInterface007 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface007,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface007 : ClaimInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 255; + int32_t interfaceid = 255; + UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + auto ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface007 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface007 : ClaimInterface-----------"); +} + +/** + * @tc.name: UsbdInterface008 + * @tc.desc: Test functions to ClaimInterface + * @tc.desc: int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:busNum、devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface008,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface008 : ClaimInterface-----------"); + uint8_t busNum = 255; + uint8_t devAddr = 255; + int32_t interfaceid = 251; + UsbdClient::ReleaseInterface(busNum,devAddr,interfaceid); + auto ret = UsbdClient::ClaimInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface008 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface008 : ClaimInterface-----------"); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdInterface011 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface011,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface011 : ReleaseInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = interface.GetId(); + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface011 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface011 : ReleaseInterface-----------"); +} + +/** + * @tc.name: UsbdInterface012 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface012,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface012 : ReleaseInterface-----------"); + uint8_t busNum = 25; + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = interface.GetId(); + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface012 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface012 : ReleaseInterface-----------"); +} + +/** + * @tc.name: UsbdInterface013 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface013,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface013 : ReleaseInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 25; + uint8_t interfaceid = interface.GetId(); + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface013 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface013 : ReleaseInterface-----------"); +} + +/** + * @tc.name: UsbdInterface014 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface014,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface014 : ReleaseInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = 255; + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface014 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface014 : ReleaseInterface-----------"); +} + +/** + * @tc.name: UsbdInterface015 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface015,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface015 : ReleaseInterface-----------"); + uint8_t busNum = 25; + uint8_t devAddr = 25; + uint8_t interfaceid = interface.GetId(); + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface015 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface015 : ReleaseInterface-----------"); +} + +/** + * @tc.name: UsbdInterface016 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:参数异常,busNum、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface016,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface016 : ReleaseInterface-----------"); + uint8_t busNum = 255; + uint8_t devAddr = device.GetDevAddr(); + int32_t interfaceid = 255; + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface016 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface016 : ReleaseInterface-----------"); +} + +/** + * @tc.name: UsbdInterface017 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface017,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface017 : ReleaseInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 255; + int32_t interfaceid = 255; + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface017 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface017 : ReleaseInterface-----------"); +} + +/** + * @tc.name: UsbdInterface018 + * @tc.desc: Test functions to ReleaseInterface + * @tc.desc: int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) + * @tc.desc: 反向测试:busNum、devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface018,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface018 : ReleaseInterface-----------"); + uint8_t busNum = 255; + uint8_t devAddr = 255; + int32_t interfaceid = 255; + auto ret = UsbdClient::ReleaseInterface(busNum, devAddr, interfaceid); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface018 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface018 : ReleaseInterface-----------"); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdInterface021 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface021,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface021 : SetInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = interface.GetId(); + uint8_t altIndex = interface.GetAlternateSetting(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer021 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface021 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface021 : SetInterface-----------"); +} + +/** + * @tc.name: UsbdInterface022 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface022,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface022 : SetInterface-----------"); + uint8_t busNum = 222; + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = interface.GetId(); + uint8_t altIndex = interface.GetAlternateSetting(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer022 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface022 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface022 : SetInterface-----------"); +} + +/** + * @tc.name: UsbdInterface023 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface023,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface023 : SetInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 222; + uint8_t interfaceid = interface.GetId(); + uint8_t altIndex = interface.GetAlternateSetting(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer023 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface023 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface023 : SetInterface-----------"); +} + +/** + * @tc.name: UsbdInterface024 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 反向测试:参数异常,interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface024,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface024 : SetInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint8_t interfaceid = 222; + uint8_t altIndex = 222; + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer024 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface024 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface024 : SetInterface-----------"); +} + +/** + * @tc.name: UsbdInterface025 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface025,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface025 : SetInterface-----------"); + uint8_t busNum = 223; + uint8_t devAddr = 223; + uint8_t interfaceid = interface.GetId(); + uint8_t altIndex = interface.GetAlternateSetting(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer025 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface025 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface025 : SetInterface-----------"); +} + +/** + * @tc.name: UsbdInterface026 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 反向测试:参数异常,busNum、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface026,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface026 : SetInterface-----------"); + uint8_t busNum = 224; + uint8_t devAddr = device.GetDevAddr(); + int32_t interfaceid = 224; + uint8_t altIndex = 1; + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer026 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface026 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface026 : SetInterface-----------"); +} + +/** + * @tc.name: UsbdInterface027 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 反向测试:devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface027,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface027 : SetInterface-----------"); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = 225; + int32_t interfaceid = 225; + uint8_t altIndex = 225; + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer027 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface027 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface027 : SetInterface-----------"); +} + +/** + * @tc.name: UsbdInterface028 + * @tc.desc: Test functions to SetInterface + * @tc.desc: int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) + * @tc.desc: 反向测试:busNum、devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdInterfaceTest,UsbdInterface028,TestSize.Level1) +{ + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case Start : UsbdInterface028 : SetInterface-----------"); + uint8_t busNum = 225; + uint8_t devAddr = 225; + int32_t interfaceid = 225; + uint8_t altIndex = 225; + uint32_t len = 0; + uint8_t buffer[255] = {}; + auto ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer028 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + ret = UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdInterfaceTest::UsbdInterface028 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "-----------Case End : UsbdInterface028 : SetInterface-----------"); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_port_test.cpp b/hdi/test/unittest/common/src/usbd_port_test.cpp new file mode 100644 index 00000000..d5a9599d --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_port_test.cpp @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_port.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_port_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbdPortTest::SetUpTestCase(void) +{ +} + +void UsbdPortTest::TearDownTestCase(void) +{ +} + +void UsbdPortTest::SetUp(void) +{ +} + +void UsbdPortTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbdPort001 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort001,TestSize.Level1) +{ + std::vector ports; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetPorts(ports); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(ports.empty())) << "ports list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort001 %{public}d size=%{public}d",__LINE__ ,ports.size()); + UsbPort *port = ports.front(); + uint32_t portId = port->id; + uint32_t powerRole = port->usbPortStatus->currentPowerRole; + uint32_t dataRole = port->usbPortStatus->currentDataRole; + ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort001 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort001 end."); +} + +/** + * @tc.name: UsbdPort002 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 反向测试:参数异常,portId错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort002,TestSize.Level1) +{ + std::vector ports; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetPorts(ports); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort002 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(ports.empty())) << "ports list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort002 %{public}d size=%{public}d",__LINE__ ,ports.size()); + UsbPort *port = ports.front(); + uint32_t portId = -1; + uint32_t powerRole = port->usbPortStatus->currentPowerRole; + uint32_t dataRole = port->usbPortStatus->currentDataRole; + ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort002 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort002 end."); +} + +/** + * @tc.name: UsbdPort003 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 反向测试:参数异常,powerRole错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort003,TestSize.Level1) +{ + std::vector ports; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetPorts(ports); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort003 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(ports.empty())) << "ports list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort003 %{public}d size=%{public}d",__LINE__ ,ports.size()); + UsbPort *port = ports.front(); + uint32_t portId = port->id; + uint32_t powerRole = -1; + uint32_t dataRole = port->usbPortStatus->currentDataRole; + ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort003 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort003 end."); +} + +/** + * @tc.name: UsbdPort004 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 反向测试:参数异常,dataRole错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort004,TestSize.Level1) +{ + std::vector ports; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetPorts(ports); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort004 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(ports.empty())) << "ports list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort004 %{public}d size=%{public}d",__LINE__ ,ports.size()); + UsbPort *port = ports.front(); + uint32_t portId = port->id; + uint32_t powerRole = port->usbPortStatus->currentPowerRole; + uint32_t dataRole = -1; + ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort004 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort004 end."); +} + +/** + * @tc.name: UsbdPort005 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 反向测试:参数异常,portId、powerRole错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort005,TestSize.Level1) +{ + std::vector ports; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetPorts(ports); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort005 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(ports.empty())) << "ports list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort005 %{public}d size=%{public}d",__LINE__ ,ports.size()); + UsbPort *port = ports.front(); + uint32_t portId = -1; + uint32_t powerRole = -1; + uint32_t dataRole = port->usbPortStatus->currentDataRole; + ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort005 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort005 end."); +} + +/** + * @tc.name: UsbdPort006 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 反向测试:参数异常,portId、dataRole错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort006,TestSize.Level1) +{ + std::vector ports; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetPorts(ports); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort006 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(ports.empty())) << "ports list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort006 %{public}d size=%{public}d",__LINE__ ,ports.size()); + UsbPort *port = ports.front(); + uint32_t portId = -1; + uint32_t powerRole = port->usbPortStatus->currentPowerRole; + uint32_t dataRole = -1; + ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort006 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort006 end."); +} + +/** + * @tc.name: UsbdPort007 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 反向测试:powerRole、dataRole错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort007,TestSize.Level1) +{ + std::vector ports; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetPorts(ports); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort007 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(ports.empty())) << "ports list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort007 %{public}d size=%{public}d",__LINE__ ,ports.size()); + UsbPort *port = ports.front(); + uint32_t portId = port->id; + uint32_t powerRole = -1; + uint32_t dataRole = -1; + ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort007 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort007 end."); +} + +/** + * @tc.name: UsbdPort008 + * @tc.desc: Test functions to SetPortRole + * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) + * @tc.desc: 反向测试:portId、powerRole、dataRole错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdPortTest,UsbdPort008,TestSize.Level1) +{ + uint32_t portId = -1; + uint32_t powerRole = -1; + uint32_t dataRole = -1; + auto ret = UsbdClient::SetPortRole(portId, powerRole, dataRole); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort008 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdPortTest::UsbdPort008 end."); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_request_test.cpp b/hdi/test/unittest/common/src/usbd_request_test.cpp new file mode 100644 index 00000000..87b726a9 --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_request_test.cpp @@ -0,0 +1,721 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_request.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_request_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbdRequestTest::SetUpTestCase(void) +{ +} + +void UsbdRequestTest::TearDownTestCase(void) +{ +} + +void UsbdRequestTest::SetUp(void) +{ +} + +void UsbdRequestTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbdRequest001 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest001,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest001 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest001 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest001 end."); +} + +/** + * @tc.name: UsbdRequest002 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest002,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest002 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest002 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = -1; + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest002 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest002 end."); +} + +/** + * @tc.name: UsbdRequest003 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest003,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest003 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest003 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest003 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest003 end."); +} + +/** + * @tc.name: UsbdRequest004 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 反向测试:参数异常,configIndex错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest004,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest004 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest004 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest004 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest004 end."); +} + +/** + * @tc.name: UsbdRequest005 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest005,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest005 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest005 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = -1; + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest005 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest005 end."); +} + +/** + * @tc.name: UsbdRequest006 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 反向测试:参数异常,busNum、interfaceId错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest006,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest006 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest006 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest006 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest006 end."); +} + +/** + * @tc.name: UsbdRequest007 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 反向测试:devAddr、interfaceId错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest007,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest007 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest007 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest007 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest007 end."); +} + +/** + * @tc.name: UsbdRequest008 + * @tc.desc: Test functions to RequestQueue + * @tc.desc: int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointid, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) + * @tc.desc: 反向测试:busNum、devAddr、interfaceId错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest008,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest008 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest008 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = -1; + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + USBDevicePipe pipe; + UsbSrvClient.OpenDevice(device, pipe); + + UsbRequest request; + request.Initialize(pipe, endpoint); + UsbSrvClient.RequestInitialize(request); + + uint8_t buffer[100] = {}; + uint32_t length = 1; + + ret = UsbdClient::RequestQueue(busNum, devAddr, interfaceId, endpointId, (void*)&request, sizeof(request), buffer, length); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest008 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest008 end."); +} + +/**********************************************************************************************************/ + +/** + * @tc.name: UsbdRequest021 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 正向测试:参数正确 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest021,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest021 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest021 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest021 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest021 end."); +} + +/** + * @tc.name: UsbdRequest022 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 反向测试:参数异常,busNum错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest022,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest022 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest022 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = -1; + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest022 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest022 end."); +} + +/** + * @tc.name: UsbdRequest023 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 反向测试:参数异常,devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest023,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest023 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest023 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest023 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest023 end."); +} + +/** + * @tc.name: UsbdRequest024 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 反向测试:参数异常,interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest024,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest024 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest024 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest024 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest024 end."); +} + +/** + * @tc.name: UsbdRequest025 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 反向测试:参数异常,busNum、devAddr错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest025,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest025 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest025 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = -1; + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = interface.GetId(); + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest025 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest025 end."); +} + +/** + * @tc.name: UsbdRequest026 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 反向测试:参数异常,busNum、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest026,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest026 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest026 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = -1; + uint8_t devAddr = device.GetDevAddr(); + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest026 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest026 end."); +} + +/** + * @tc.name: UsbdRequest027 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 反向测试:参数异常,devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest027,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest027 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest027 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest027 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest027 end."); +} + +/** + * @tc.name: UsbdRequest028 + * @tc.desc: Test functions to RequestCancel + * @tc.desc: int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid) + * @tc.desc: 反向测试:参数异常,busNum、devAddr、interfaceid错误 + * @tc.type: FUNC + */ +HWTEST_F(UsbdRequestTest,UsbdRequest028,TestSize.Level1) +{ + std::vector deviceList; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest028 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "device list NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest028 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + UsbDevice device = deviceList.front(); + uint8_t busNum = -1; + uint8_t devAddr = -1; + std::vector configs = device.GetConfigs(); + ASSERT_TRUE(!(configs.empty())) << "config list NULL"; + USBConfig config = configs.front(); + std::vector interfaces = config.GetInterfaces(); + ASSERT_TRUE(!(interfaces.empty())) << "interface list NULL"; + UsbInterface interface = interfaces.front(); + uint8_t interfaceId = -1; + std::vector endpoints = interface.GetEndpoints(); + ASSERT_TRUE(!(endpoints.empty())) << "endpoint list NULL"; + USBEndpoint endpoint = endpoints.front(); + uint8_t endpointId = endpoint.GetNumber(); + + ret = UsbdClient::RequestCancel(busNum, devAddr, interfaceId, endpointId); + ASSERT_TRUE(ret != ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest028 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdRequestTest::UsbdRequest028 end."); +} \ No newline at end of file diff --git a/hdi/test/unittest/common/src/usbd_transfer_test.cpp b/hdi/test/unittest/common/src/usbd_transfer_test.cpp new file mode 100644 index 00000000..3c560887 --- /dev/null +++ b/hdi/test/unittest/common/src/usbd_transfer_test.cpp @@ -0,0 +1,2349 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "usbd_client.h" +#include "usbd_config_test.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include +#include +#include "usbd_transfer_test.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbdTransferTest::SetUpTestCase(void) +{ + +} + +void UsbdTransferTest::TearDownTestCase(void) +{ + +} + +void UsbdTransferTest::SetUp(void) +{ + +} + +void UsbdTransferTest::TearDown(void) +{ + +} + + + +/** + * @tc.name: UsbdControlTransfer001 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer001,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer001 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer001 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer001 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer001 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer001 end."); +} + +/** + * @tc.name: UsbdControlTransfer002 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer002,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer002 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer002 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer003 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000001,1,1,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer002 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer002 end."); +} + +/** + * @tc.name: UsbdControlTransfer003 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer003,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer003 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer003 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer003 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000010,1,1,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer003 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer003 end."); +} + +/** + * @tc.name: UsbdControlTransfer004 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer004,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer004 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer004 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer004 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 1; + const USBConfig& config = device.GetConfigs().front(); + uint8_t configId = config.GetId(); + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,8,0,0,&configId,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer004 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer004 end."); +} + +/** + * @tc.name: UsbdControlTransfer005 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer005,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer005 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer005 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer005 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 8; + uint8_t buffer[255] = {}; + memset(buffer,0,255); + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,6,0x100,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer005 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer005 end."); +} + +/** + * @tc.name: UsbdControlTransfer006 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer006,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer006 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer006 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer006 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = device.GetConfigs().front().GetInterfaces().front(); + int32_t intercafeidex = interface.GetiInterface(); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 8; + uint8_t *buffer = (uint8_t *)interface.GetAlternateSetting(); + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000001,0X0A,0,intercafeidex,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer006 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer006 end."); +} + +/** + * @tc.name: UsbdControlTransfer007 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer007,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer007 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer007 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer007 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 2; + uint8_t buffer[10] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,0,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer007 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer007 end."); +} + +/** + * @tc.name: UsbdControlTransfer008 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer008,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer008 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer008 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer008 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 2; + uint8_t buffer[10] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000001,0,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer008 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer008 end."); +} + +/** + * @tc.name: UsbdControlTransfer009 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer009,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer009 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer009 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer009 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 2; + uint8_t buffer[10] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000010,0,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer009 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer009 end."); +} + +/** + * @tc.name: UsbdControlTransfer010 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer010,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer010 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer010 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer010 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,5,devAddr,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer010 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer010 end."); +} + +/** + * @tc.name: UsbdControlTransfer011 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer011,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer011 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer011 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer011 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + const USBConfig& config = device.GetConfigs().front(); + uint8_t configId = config.GetId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer012 %{public}d ControlTransfer=%{public}d",__LINE__ ,configId); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,9,configId,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer011 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer011 end."); +} + +/** + * @tc.name: UsbdControlTransfer012 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer012,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer012 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer012 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer012 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[255] = {}; + memset(buffer,0,255); + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,7,0X100,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer012 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer012 end."); +} + +/** + * @tc.name: UsbdControlTransfer013 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer013,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer013 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer013 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer013 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000000,3,3,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer013 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer013 end."); +} + +/** + * @tc.name: UsbdControlTransfer014 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer014,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer014 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer014 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer014 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000001,3,3,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer014 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer014 end."); +} + +/** + * @tc.name: UsbdControlTransfer015 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer015,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer015 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer015 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer015 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000010,3,3,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer015 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer015 end."); +} + +/** + * @tc.name: UsbdControlTransfer016 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer016,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer016 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer016 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer016 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = device.GetConfigs().front().GetInterfaces().front(); + int32_t alter = interface.GetAlternateSetting(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer016 %{public}d alter=%{public}d",__LINE__ ,alter); + int32_t interfaceindex = interface.GetiInterface(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer016 %{public}d interfaceindex=%{public}d",__LINE__ ,interfaceindex); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b00000001,0X0B,alter,interfaceindex,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer016 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer016 end."); +} + +/** + * @tc.name: UsbdControlTransfer017 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer017,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer017 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer017 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer017 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + int idex = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().front().GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer017 %{public}d idex=%{public}d",__LINE__ ,idex); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 2; + uint8_t buffer[] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000010,0X0C,0,idex,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer017 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer017 end."); +} + +/** + * @tc.name: UsbdControlTransfer018 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer018,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer018 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer018 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer018 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 255; + uint8_t buffer[255] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0XFFFF,1,1,0,buffer,len,500); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer018 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdConfigTest::UsbdControlTransfer018 end."); +} + +/** + * @tc.name: UsbdControlTransfer019 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer019,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer019 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer019 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer019 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 1; + const USBConfig& config = device.GetConfigs().front(); + uint8_t configId = config.GetId(); + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,0X0F,0,0,&configId,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer019 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer019 end."); +} + +/** + * @tc.name: UsbdControlTransfer020 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer020,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer020 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer020 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer020 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 255; + uint8_t buffer[255] = {}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,11,0X7FFFFFFF,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer020 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer020 end."); +} + +/** + * @tc.name: UsbdControlTransfer021 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer021,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer021 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer021 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer021 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,7,0,0b11111111,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer021 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer021 end."); +} + +/** + * @tc.name: UsbdControlTransfer022 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer022,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer022 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer022 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer022 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,7,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer022 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer022 end."); +} + +/** + * @tc.name: UsbdControlTransfer023 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer023,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer023 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer023 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer023 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 0; + uint8_t buffer[100] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,7,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer023 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer023 end."); +} + +/** + * @tc.name: UsbdControlTransfer024 + * @tc.desc: Test functions to ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, + * int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdControlTransfer024,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer024 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer024 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer024 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::ControlTransfer(busNum,devAddr,0b10000000,7,0,0,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer024 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdControlTransfer024 end."); +} + +/** + * @tc.name: UsbdBulkTransferRead030 + * @tc.desc: Test functions to BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferRead030,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead030 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead030 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead030 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().front(); + uint8_t interfaceid = point.GetInterfaceId(); + uint8_t pionid = point.GetEndpointNumber(); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead030 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead030 end."); +} + +/** + * @tc.name: UsbdBulkTransferRead031 + * @tc.desc: Test functions to BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferRead031,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead031 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead031 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead031 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().front(); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead031 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead031 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead031 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead031 end."); +} + +/** + * @tc.name: UsbdBulkTransferRead032 + * @tc.desc: Test functions to BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferRead032,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead032 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead032 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead032 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead032 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead032 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead032 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead032 end."); +} + +/** + * @tc.name: UsbdBulkTransferRead033 + * @tc.desc: Test functions to BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferRead033,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead033 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead033 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead033 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead033 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead033 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead033 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead033 end."); +} + +/** + * @tc.name: UsbdBulkTransferRead034 + * @tc.desc: Test functions to BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferRead034,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead034 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead034 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead034 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead034 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead034 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead034 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead034 end."); +} + +/** + * @tc.name: UsbdBulkTransferRead035 + * @tc.desc: Test functions to BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferRead035,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead035 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead035 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead035 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead035 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead035 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead035 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferRead035 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite040 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite040,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite040 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite040 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite040 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite040 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite040 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite040 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite040 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite041 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite041,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite041 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite041 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite041 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite041 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite041 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite041 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite041 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite042 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite042,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite042 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite042 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite042 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite042 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite042 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite042 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite042 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite043 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite043,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite043 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite043 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite043 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite043 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite043 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite043 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite043 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite044 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite044,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite044 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite044 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite044 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite044 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite044 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite044 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite044 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite045 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite045,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite045 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite045 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite045 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite045 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite045 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite045 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite045 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite046 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite046,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite046 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite046 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite046 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite046 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite046 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[100] = {}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite046 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite046 end."); +} + +/** + * @tc.name: UsbdBulkTransferWrite047 + * @tc.desc: Test functions to BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdBulkTransferWrite047,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite047 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite047 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite047 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite047 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite047 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {}; + ret = UsbdClient::BulkTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite047 %{public}d UsbdBulkTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdBulkTransferWrite047 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead050 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,InterruptTransferRead050,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::InterruptTransferRead050 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::InterruptTransferRead050 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::InterruptTransferRead050 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::InterruptTransferRead050 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::InterruptTransferRead050 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::InterruptTransferRead050 %{public}d UsbdInterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::InterruptTransferRead050 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead051 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead051,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead051 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead051 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead051 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead051 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead051 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead051 %{public}d InterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead051 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead052 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead052,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead052 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead052 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead052 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead052 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead052 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead052 %{public}d InterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead052 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead053 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead053,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead053 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead053 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead053 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead053 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead053 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead053 %{public}d InterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead053 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead054 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead054,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead054 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead054 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead054 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead054 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead054 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead054 %{public}d InterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead054 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead055 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead055,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead055 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead055 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead055 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead055 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead055 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead055 %{public}d InterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead055 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead056 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead056,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead056 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead056 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead056 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead056 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead056 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[100] = {}; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead056 %{public}d InterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead056 end."); +} + +/** + * @tc.name: UsbdInterruptTransferRead057 + * @tc.desc: Test functions to InterruptTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead057,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead057 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead057 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead057 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead057 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead057 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {}; + ret = UsbdClient::InterruptTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead057 %{public}d InterruptTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferRead057 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite060 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferWrite060,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite060 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite060 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite060 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite060 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite060 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite060 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite060 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite061 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferWrite061,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite061 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite061 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite061 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite061 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite061 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite061 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite061 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite062 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferWrite062,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite062 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite062 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite062 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite062 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite062 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite062 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite062 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite063 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferRead063,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite063 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite063 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite063 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite063 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite063 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite063 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite063 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite064 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferWrite064,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite064 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite064 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite064 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite064 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite064 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite064 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite064 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite065 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferWrite065,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite065 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite065 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite065 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite065 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite065 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite065 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite065 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite066 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferWrite066,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite066 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite066 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite066 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite066 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite066 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[100] = {}; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite066 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite066 end."); +} + +/** + * @tc.name: UsbdInterruptTransferWrite067 + * @tc.desc: Test functions to InterruptTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdInterruptTransferWrite067,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite067 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite067 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite067 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite067 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite067 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {}; + ret = UsbdClient::InterruptTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite067 %{public}d InterruptTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdInterruptTransferWrite067 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead070 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead070,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead070 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead070 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead070 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead070 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead070 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead070 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead070 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead071 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead071,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead071 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead071 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead071 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead071 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead071 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead071 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead071 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead072 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead072,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead072 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead072 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead072 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead072 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead072 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead072 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead072 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead073 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead073,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead073 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead073 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead073 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead073 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead073 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead073 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead073 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead074 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead074,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead074 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead074 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead074 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead074 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead074 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead074 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead074 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead075 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead075,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead075 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead075 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead075 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead075 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead075 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead075 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead075 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead076 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead076,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead076 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead076 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead076 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead076 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead076 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[100] = {}; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead076 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead076 end."); +} + +/** + * @tc.name: UsbdIsoTransferRead077 + * @tc.desc: Test functions to IsoTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferRead077,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead077 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead077 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead077 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead077 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead077 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {}; + ret = UsbdClient::IsoTransferRead(busNum,devAddr,interfaceid,pionid,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead077 %{public}d IsoTransferRead=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferRead077 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite080 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite080,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite080 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite080 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite080 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite080 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite080 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite080 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite080 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite081 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite081,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite081 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite081 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite081 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite081 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite081 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite081 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite081 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite082 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite082,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite082 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite082 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite082 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite082 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite082 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite082 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite082 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite083 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite083,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite083 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite083 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite083 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(0); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite083 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite083 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite083 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite083 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite084 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite084,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite084 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite084 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite084 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite084 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite084 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {0}; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite084 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite084 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite085 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite085,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite085 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite085 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite085 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite085 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite085 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite085 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite085 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite086 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite086,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite086 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite086 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite086 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite086 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite086 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[100] = {}; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite086 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite086 end."); +} + +/** + * @tc.name: UsbdIsoTransferWrite087 + * @tc.desc: Test functions to IsoTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F(UsbdTransferTest,UsbdIsoTransferWrite087,TestSize.Level1) +{ + std::vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite087 %{public}d GetDevices=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite087 %{public}d size=%{public}d",__LINE__ ,devi.size()); + UsbDevice device = devi.front(); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite087 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint8_t busNum = device.GetBusNum(); + uint8_t devAddr = device.GetDevAddr(); + USBEndpoint point = device.GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + uint8_t interfaceid = point.GetInterfaceId(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite087 %{public}d interfaceid=%{public}d",__LINE__ ,ret); + uint8_t pionid = point.GetEndpointNumber(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite087 %{public}d pionid=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[100] = {}; + ret = UsbdClient::IsoTransferWrite(busNum,devAddr,interfaceid,pionid,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite087 %{public}d IsoTransferWrite=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbdTransferTest::UsbdIsoTransferWrite087 end."); +} \ No newline at end of file diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn new file mode 100644 index 00000000..41958106 --- /dev/null +++ b/interfaces/innerkits/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") + +config("usbsrv_private_config") { + include_dirs = [ + "${usb_manager_path}/services/zidl/include", + "//utils/system/safwk/native/include", + ] +} + +config("usbsrv_public_config") { + include_dirs = [ "native/include" ] +} + +ohos_shared_library("usbsrv_client") { + sources = [ + "${usb_manager_path}/services/zidl/src/usb_srv_proxy.cpp", + "native/src/usb_srv_client.cpp", + "native/src/usb_device.cpp", + "native/src/usb_pipe.cpp", + "native/src/usb_request.cpp", + ] + + configs = [ + "${utils_path}:utils_config", + ":usbsrv_private_config", + ":usbsrv_public_config", + ] + + public_configs = [ ":usbsrv_public_config" ] + + deps = [ "//utils/native/base:utils" ] + + external_deps = [ + "appexecfwk_standard:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + + part_name = "usb_manager_native" +} diff --git a/interfaces/innerkits/native/include/iusb_srv.h b/interfaces/innerkits/native/include/iusb_srv.h new file mode 100644 index 00000000..e0eafaf1 --- /dev/null +++ b/interfaces/innerkits/native/include/iusb_srv.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBMGR_INNERKITS_IUSB_SRV_H +#define USBMGR_INNERKITS_IUSB_SRV_H + +#include +#include "iremote_broker.h" +#include "iremote_object.h" +#include "usb_info.h" +#include "usb_port.h" +#include "usb_device.h" +#include + +namespace OHOS { +namespace USB { + +class IUsbSrv : public IRemoteBroker { +public: + enum { + USB_FUN_HAS_RIGHT = 0, + USB_FUN_REQUEST_RIGHT, + USB_FUN_OPEN_DEVICE, + USB_FUN_GET_DEVICE, + USB_FUN_GET_DEVICES, + USB_FUN_GET_CURRENT_FUNCTIONS, + USB_FUN_SET_CURRENT_FUNCTIONS, + USB_FUN_USB_FUNCTIONS_FROM_STRING, + USB_FUN_USB_FUNCTIONS_TO_STRING, + USB_FUN_CLAIM_INTERFACE, + USB_FUN_RELEASE_INTERFACE, + USB_FUN_BULK_TRANSFER_READ, + USB_FUN_BULK_TRANSFER_WRITE, + USB_FUN_CONTROL_TRANSFER, + USB_FUN_SET_ACTIVE_CONFIG, + USB_FUN_GET_ACTIVE_CONFIG, + USB_FUN_SET_INTERFACE, + USB_FUN_GET_PORTS, + USB_FUN_GET_SUPPORTED_MODES, + USB_FUN_SET_PORT_ROLE, + USB_FUN_REQUEST_QUEUE, + USB_FUN_REQUEST_WAIT, + USB_FUN_REQUEST_CANCEL, + USB_FUN_GET_DESCRIPTOR, + USB_FUN_CLOSE_DEVICE, + }; + + virtual int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) = 0; + virtual int32_t HasRight(std::string deviceName) = 0; + virtual int32_t RequestRight(std::string deviceName) = 0; + virtual int32_t GetDevices(std::vector &deviceList) = 0; + virtual int32_t GetDevice(uint8_t busNum, uint8_t devAddr, UsbDevice &devInfo) = 0; + virtual int32_t GetCurrentFunctions() = 0; + virtual bool SetCurrentFunctions(int32_t funcs) = 0; + virtual int32_t UsbFunctionsFromString(std::string funcs) = 0; + virtual std::string UsbFunctionsToString(int32_t funcs) = 0; + virtual int32_t GetPorts(std::vector &result) = 0; + virtual int32_t GetSupportedModes(int32_t portId, int32_t &result) = 0; + virtual int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) = 0; + virtual int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) = 0; + virtual int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) = 0; + virtual int32_t BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, uint8_t *buffer, uint32_t &length, int32_t timeout) = 0; + virtual int32_t BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointid, const uint8_t *buffer, uint32_t length, int32_t timeout) = 0; + virtual int32_t ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout) = 0; + virtual int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configId) = 0; + virtual int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configId) = 0; + virtual int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) = 0; + virtual int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *desc, uint32_t &length) = 0; + virtual int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointId, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) = 0; + virtual int32_t RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, void *clientData, uint32_t &clientLength, uint8_t *buffer, uint32_t &length) = 0; + virtual int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) = 0; + virtual int32_t Close(uint8_t busNum, uint8_t devAddr) = 0; + +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.usb.IUsbSrv"); +}; + +} // space USB +} // namespace OHOS + +#endif // USBMGR_INNERKITS_IUSB_SRV_H diff --git a/interfaces/innerkits/native/include/usb_config.h b/interfaces/innerkits/native/include/usb_config.h new file mode 100644 index 00000000..5abfef9c --- /dev/null +++ b/interfaces/innerkits/native/include/usb_config.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_CONFIG_H +#define USB_CONFIG_H + +#include +#include +#include +#include +#include +#include "usb_common.h" +#include "usb_interface.h" + +namespace OHOS +{ + namespace USB + { + class USBConfig + { + public: + USBConfig(uint32_t id, uint32_t attributes, std::string name, uint32_t maxPower, std::vector interfaces) + { + this->id = id; + this->attributes = attributes; + this->maxPower = maxPower; + this->name = name; + this->interfaces = interfaces; + } + USBConfig(){} + + const int32_t &GetId() const + { + return id; + } + + const int32_t &GetAttributes() const + { + return attributes; + } + + bool GetInterface(uint32_t index, UsbInterface &interface) const + { + if(index >= interfaces.size()) { + return false; + } + interface = interfaces[index]; + return true; + } + + int32_t GetInterfaceCount() const + { + return interfaces.size(); + } + + int32_t GetMaxPower() const + { + return maxPower * 2; + } + + const std::string &GetName() const + { + return name; + } + + bool IsRemoteWakeup() const + { + return (attributes & USB_CFG_REMOTE_WAKEUP) != 0; + } + + bool IsSelfPowered() const + { + return (attributes & USB_CFG_SELF_POWERED) != 0; + } + + void SetInterfaces(const std::vector &interfaces) + { + this->interfaces = interfaces; + } + std::vector &GetInterfaces(){ + return interfaces; + } + void SetId(int32_t id) { + this->id = id; + } + void SetAttribute(int32_t attributes) { + this->attributes = attributes; + } + void SetMaxPower(int32_t maxPower) { + this->maxPower = maxPower; + } + std::string ToString() const { + std::string str; + std::ostringstream ss; + ss << "name=" << name << "," + << "id=" << id << "," + << "iConfiguration=" << (int32_t)iConfiguration << "," + << "attributes=" << attributes << "," + << "maxPower=" << maxPower << "; "; + str = ss.str(); + ss.str(""); + for(size_t i = 0; i < interfaces.size(); ++i) { + const UsbInterface &interface = interfaces[i]; + str += interface.ToString(); + } + return str; + } + void SetName(std::string Name){ + this->name = Name; + } + void SetiConfiguration(uint8_t idx){ + this->iConfiguration = idx; + } + uint8_t GetiConfiguration(){ + return this->iConfiguration; + } + private: + int32_t id = INVALID_USB_INT_VALUE; + int32_t attributes = INVALID_USB_INT_VALUE; + std::vector interfaces; + int32_t maxPower = INVALID_USB_INT_VALUE; + std::string name; + uint8_t iConfiguration; + }; + } // namespace USB +} // namespace OHOS + +#endif // USB_CONFIG_H \ No newline at end of file diff --git a/interfaces/innerkits/native/include/usb_device.h b/interfaces/innerkits/native/include/usb_device.h new file mode 100644 index 00000000..e7725d76 --- /dev/null +++ b/interfaces/innerkits/native/include/usb_device.h @@ -0,0 +1,186 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_DEVICE_H +#define USB_DEVICE_H + +#include "iremote_object.h" +#include +#include +#include "usb_config.h" +#include "usb_interface.h" +#include +#include +#include + +namespace OHOS { +namespace USB { +class UsbDevice { + public: + UsbDevice(std::string mName, std::string mManufacturerName, + std::string mProductName, std::string mVersion, uint8_t devAddr, + uint8_t busNum, int mVendorId, int mProductId, int mClass, + int mSubclass, int mProtocol, std::vector configs) { + this->mName = mName; + this->mManufacturerName = mManufacturerName; + this->mProductName = mProductName; + this->mVersion = mVersion; + this->devAddr = devAddr; + this->busNum = busNum; + this->mVendorId = mVendorId; + this->mProductId = mProductId; + this->mClass = mClass; + this->mSubclass = mSubclass; + this->mProtocol = mProtocol; + this->configs = configs; + } + UsbDevice() {} + const std::string &GetName() const { return mName; } + const std::string &GetManufacturerName() const { return mManufacturerName; } + const std::string &GetProductName() const { return mProductName; } + const std::string &GetVersion() const { return mVersion; } + int32_t GetVendorId() const { return mVendorId; } + int32_t GetProductId() const { return mProductId; } + int32_t GetClass() const { return mClass; } + int32_t GetSubclass() const { return mSubclass; } + int32_t GetProtocol() const { return mProtocol; } + int32_t GetConfigCount() const { return configs.size(); } + int32_t GetConfig(uint32_t index, USBConfig &config) const { + if (index >= configs.size()) { + return ERR_INVALID_VALUE; + } + config = configs[index]; + return ERR_OK; + } + void SetConfigs(const std::vector &configs) { + this->configs = configs; + } + + uint8_t GetDevAddr() const { return devAddr; } + uint8_t GetBusNum() const { return busNum; } + bool GetDescConfigCount() { return descConfigCount; } + + void SetDevAddr(uint8_t Num) { devAddr = Num; } + void SetBusNum(uint8_t Num) { busNum = Num; } + void SetName(std::string Name) { mName = Name; } + void SetManufacturerName(std::string Name) { mManufacturerName = Name; } + void SetProductName(std::string Name) { mProductName = Name; } + void SetVersion(std::string Name) { mVersion = Name; } + void SetVendorId(int Id) { mVendorId = Id; } + void SetProductId(int Id) { mProductId = Id; } + void SetClass(int Id) { mClass = Id; } + void SetSubclass(int Id) { mSubclass = Id; } + void SetProtocol(int Id) { mProtocol = Id; } + void SetDescConfigCount(uint8_t count) { descConfigCount = count; } + + std::vector &GetConfigs() { return configs; } + + std::string ToString() const { + std::string str; + std::ostringstream ss; + ss << "mName=" << mName << "," + << "mManufacturerName=" << mManufacturerName << "," + << "mProductName=" << mProductName << "," + << "mVersion=" << mVersion << "," + << "mSerial=" << mSerial << "," + << "busNum=" << (int32_t)busNum << "," + << "devAddr=" << (int32_t)devAddr << "," + << "mVendorId=" << mVendorId << "," + << "mProductId=" << mProductId << "," + << "mClass=" << mClass << "," + << "mSubclass=" << mSubclass << "," + << "mProtocol=" << mProtocol << "; "; + str = ss.str(); + ss.str(""); + std::string strConfigs; + for(size_t i = 0; i < configs.size(); ++i) { + const USBConfig &config = configs[i]; + strConfigs += config.ToString(); + } + str += strConfigs; + return str; + } + + void SetiManufacturer(uint8_t idx){ + this->iManufacturer = idx; + } + uint8_t GetiManufacturer(){ + return this->iManufacturer; + } + void SetiProduct(uint8_t idx){ + this->iProduct = idx; + } + uint8_t GetiProduct(){ + return this->iProduct; + } + void SetiSerialNumber(uint8_t idx){ + this->iSerialNumber = idx; + } + uint8_t GetiSerialNumber(){ + return this->iSerialNumber; + } + void SetmSerial(std::string Name){ + this->mSerial = Name; + } + std::string GetmSerial(){ + return this->mSerial; + } + void SetbMaxPacketSize0(uint8_t val){ + this->bMaxPacketSize0 = val; + } + uint8_t GetbMaxPacketSize0(){ + return this->bMaxPacketSize0; + } + void SetbcdUSB(uint16_t val){ + this->bcdUSB = val; + } + uint16_t GetbcdUSB(){ + return this->bcdUSB; + } + void SetbcdDevice(uint16_t val){ + this->bcdDevice = val; + } + uint16_t GetbcdDevice(){ + return this->bcdDevice; + } + + private: + std::string mName; + std::string mManufacturerName; + std::string mProductName; + std::string mVersion; + std::string mSerial; + uint8_t devAddr; + uint8_t busNum; + uint8_t descConfigCount; + + uint8_t bMaxPacketSize0; + uint16_t bcdUSB; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + + int mVendorId; + int mProductId; + int mClass; + int mSubclass; + int mProtocol; + std::vector configs; +}; +} // namespace USB +} // namespace OHOS + +#endif // USB_DEVICE_H diff --git a/interfaces/innerkits/native/include/usb_endpoint.h b/interfaces/innerkits/native/include/usb_endpoint.h new file mode 100644 index 00000000..b559631f --- /dev/null +++ b/interfaces/innerkits/native/include/usb_endpoint.h @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_ENDPOINT_H +#define USB_ENDPOINT_H + +#include +#include +#include +#include "usb_common.h" + +namespace OHOS +{ + namespace USB + { + class USBEndpoint + { + public: + USBEndpoint(uint32_t address, uint32_t attributes, uint32_t interval, uint32_t maxPacketSize) + { + this->address = address; + this->attributes = attributes; + this->interval = interval; + this->maxPacketSize = maxPacketSize; + } + USBEndpoint(){} + + uint8_t GetNumber() const { + return address & USB_ENDPOINT_NUMBER_MASK; + } + const int32_t &GetAddress() const + { + return address; + } + + int32_t GetDirection() const + { + return address & USB_ENDPOINT_DIR_MASK; + } + + const int32_t &GetAttributes() const + { + return attributes; + } + + int32_t GetEndpointNumber() const + { + return address & USB_ENDPOINT_NUMBER_MASK; + } + + const int32_t &GetInterval() const + { + return interval; + } + + const int32_t &GetMaxPacketSize() const + { + return maxPacketSize; + } + + int32_t GetType() const + { + return attributes & USB_ENDPOINT_XFERTYPE_MASK; + } + + std::string ToString() const + { + std::string ret = "USBEndpoint:[Address:"; + ret.append(std::to_string(address)) + .append(", Direction:") + .append(std::to_string(GetDirection())) + .append(", Attributes:") + .append(std::to_string(attributes)) + .append(", EndpointNumber:") + .append(std::to_string(GetEndpointNumber())) + .append(", Interval:") + .append(std::to_string(interval)) + .append(", MaxPacketSize:") + .append(std::to_string(maxPacketSize)) + .append(", Type:") + .append(std::to_string(GetType())) + .append("]"); + return ret; + } + void SetAddr(int32_t val){ + address = val; + } + void SetAttr(int32_t val){ + attributes = val; + } + void SetInterval(int32_t val){ + interval = val; + } + void SetMaxPacketSize(int32_t val){ + maxPacketSize = val; + } + void SetInterfaceId(uint8_t interfaceId) { + this->interfaceId = interfaceId; + } + int8_t GetInterfaceId() const { + return interfaceId; + } + private: + int32_t address = INVALID_USB_INT_VALUE; + int32_t attributes = INVALID_USB_INT_VALUE; + int32_t interval = INVALID_USB_INT_VALUE; + int32_t maxPacketSize = INVALID_USB_INT_VALUE; + uint8_t interfaceId; + }; + } // namespace USB +} // namespace OHOS + +#endif // USB_ENDPOINT_H diff --git a/interfaces/innerkits/native/include/usb_info.h b/interfaces/innerkits/native/include/usb_info.h new file mode 100644 index 00000000..d632ec82 --- /dev/null +++ b/interfaces/innerkits/native/include/usb_info.h @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_INFO_H +#define USB_INFO_H + +#include +#include + +namespace OHOS { +namespace USB { +static constexpr int32_t INVALID_BATT_INT_VALUE = -1; +static constexpr int32_t INVALID_BATT_TEMP_VALUE = 100; +//static const std::string INVALID_STRING_VALUE = "Invalid"; +static constexpr bool INVALID_BATT_BOOL_VALUE = true; + +struct FoundDevice { + int i; +}; + +struct USBPortStatusInfo +{ + int currentMode; + int currentPowerRole; + int currentDataRole; +}; + +struct USBPortInfo +{ + int id; + int supportedModes; + USBPortStatusInfo status; +}; + +struct USBDeviceInfo +{ + int status; + int busNum; + int devNum; +}; + +class UsbInfo { +public: + + void SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) const + { + + } + + void setDevInfoStatus(int status) + { + devInfo.status = status; + } + void setDevInfoBusNum(int busNum) + { + devInfo.busNum = busNum; + } + void setDevInfoDevNum(int devNum) + { + devInfo.devNum = devNum; + } + + + int getDevInfoStatus() const + { + return devInfo.status; + } + int getDevInfoBusNum() const + { + return devInfo.busNum; + } + int getDevInfoDevNum() const + { + return devInfo.devNum; + } + +private: + // USBPortInfo port; + USBDeviceInfo devInfo; +}; +} // namespace USB +} // namespace OHOS + +#endif // USBMGR_USB_INFO_H diff --git a/interfaces/innerkits/native/include/usb_interface.h b/interfaces/innerkits/native/include/usb_interface.h new file mode 100644 index 00000000..1d363abf --- /dev/null +++ b/interfaces/innerkits/native/include/usb_interface.h @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_INTERFACE_H +#define USB_INTERFACE_H + +#include +#include +#include +#include +#include "usb_endpoint.h" + +namespace OHOS { + namespace USB { + class UsbInterface { + public: + UsbInterface(int32_t mId, int32_t mProtocol, int32_t mClass, int32_t mSubClass, int32_t mAlternateSetting, + std::string mName, std::vector mEndpoints){ + this->mId = mId; + this->mProtocol = mProtocol; + this->mClass = mClass; + this->mSubClass = mSubClass; + this->mAlternateSetting = mAlternateSetting; + this->mEndpoints = mEndpoints; + } + UsbInterface(){} + const std::string& GetName() const { + return mName; + } + int32_t GetId() const { + return mId; + } + int32_t GetClass() const { + return mClass; + } + int32_t GetSubClass() const { + return mSubClass; + } + int32_t GetAlternateSetting() const { + return mAlternateSetting; + } + int32_t GetProtocol() const { + return mProtocol; + } + int32_t GetEndpointCount() const { + return mEndpoints.size(); + } + void GetEndpoint(uint32_t index, USBEndpoint &ep) const { + if(index < mEndpoints.size()) { + ep = mEndpoints[index]; + } + } + std::vector& GetEndpoints() { + return mEndpoints; + } + void SetEndpoints(const std::vector &eps) { + mEndpoints = eps; + } + //void setEndpoints(ohos.usb.USBEndpoint[]) + // void AddEndPoint(USBEndpoint *ep){ + // mEndpoints.push_back(ep); + // } + void SetId(int Val){ + mId = Val; + } + void SetProtocol(int Val){ + mProtocol = Val; + } + void SetClass(int Val){ + mClass = Val; + } + void SetSubClass(int Val){ + mSubClass = Val; + } + void SetAlternateSetting(int Val){ + mAlternateSetting = Val; + } + void SetName(std::string Name){ + mName = Name; + } + ~UsbInterface(){} + std::string ToString() const { + std::string str; + std::ostringstream ss; + ss << "id=" << mId << "," + << "mName=" << mName << "," + << "iInterface=" << (int32_t)iInterface << "," + << "mClass=" << mClass << "," + << "mSubClass=" << mSubClass << "," + << "mProtocol=" << mProtocol << "," + << "mAlternateSetting=" << mAlternateSetting << "; "; + str = ss.str(); + ss.str(""); + for(size_t i = 0; i < mEndpoints.size(); ++i) { + const USBEndpoint &endpoint = mEndpoints[i]; + str += endpoint.ToString(); + } + return str; + } + void SetiInterface(uint8_t idx){ + this->iInterface = idx; + } + uint8_t GetiInterface(){ + return this->iInterface; + } + private: + int mId; + int mProtocol; + int mClass; + int mSubClass; + int mAlternateSetting; + std::string mName; + std::vector mEndpoints; + uint8_t iInterface; + + }; + }// namespace PowerMgr +} // namespace OHOS + +#endif // USB_INTERFACE_H + + diff --git a/interfaces/innerkits/native/include/usb_pipe.h b/interfaces/innerkits/native/include/usb_pipe.h new file mode 100644 index 00000000..aa8e1c1f --- /dev/null +++ b/interfaces/innerkits/native/include/usb_pipe.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_PIPE_H +#define USB_PIPE_H + +#include +#include +#include "iusb_srv.h" +#include "iremote_object.h" + +namespace OHOS { +namespace USB { +class UsbRequest; +class UsbSrvClient; +class USBDevicePipe { + public: + USBDevicePipe(); + USBDevicePipe(uint8_t busNum, uint8_t devAddr, int32_t fd); + int32_t ClaimInterface(const UsbInterface &interface, bool force); + int32_t ReleaseInterface(const UsbInterface &interface); + int32_t BulkTransfer(const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout); + int32_t ControlTransfer(int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, + int32_t timeout); + int32_t SetConfiguration(const USBConfig &config); + int32_t SetInterface(const UsbInterface &interface); + int32_t GetRawDescriptors(uint8_t *buffer, uint32_t &length); + int32_t GetFileDescriptor() const; + int32_t Close(); + int32_t RequestWait(int32_t timeout, UsbRequest &req); + + void SetBusNum(uint8_t busNum); + void SetDevAddr(uint8_t devAddr); + void SetFd(int32_t fd); + void SetSerial(const std::string &serial); + uint8_t GetBusNum() const; + uint8_t GetDevAddr() const; + uint8_t GetFd() const; + std::string GetSerial() const; + + private: + uint8_t busNum; + uint8_t devAddr; + int32_t fd; + std::string serial; + UsbSrvClient *usbClient; +}; +} // namespace USB +} // namespace OHOS + +#endif // USB_PIPE_H diff --git a/interfaces/innerkits/native/include/usb_port.h b/interfaces/innerkits/native/include/usb_port.h new file mode 100644 index 00000000..91086dde --- /dev/null +++ b/interfaces/innerkits/native/include/usb_port.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_PORT_H +#define USB_PORT_H + +#include "usb_common.h" +#include "usb_port_status.h" +#include +#include + +namespace OHOS { +namespace USB { +// class UsbPort { +// public: +// UsbPort(uint32_t id, uint32_t supportedModes, +// UsbPortStatus *usbPortStatus) { +// mId = id; +// mSupportedModes = supportedModes; +// mUsbPortStatus = usbPortStatus; +// } +// UsbPort(); +// ~UsbPort(); + +// uint32_t getId() const { return mId; } +// uint32_t getSupportedModes() const { return mSupportedModes; } +// UsbPortStatus *getUsbPortStatus() const { return mUsbPortStatus; } + +// private: +// uint32_t mId = INVALID_USB_INT_VALUE; +// uint32_t mSupportedModes = INVALID_USB_INT_VALUE; +// UsbPortStatus *mUsbPortStatus; +// }; + +struct UsbPortStatus { + int32_t currentMode; + /** + * POWER_ROLE_NONE, + * POWER_ROLE_SOURCE, + * POWER_ROLE_SINK + */ + int32_t currentPowerRole; + /** + * DATA_ROLE_NONE, + * DATA_ROLE_HOST, + * DATA_ROLE_DEVICE + */ + int32_t currentDataRole; +}; +struct UsbPort { + int32_t id; + /** + * MODE_NONE + * MODE_DFP + * MODE_UFP + * MODE_DUAL + */ + int32_t supportedModes; + UsbPortStatus *usbPortStatus; +}; + +} // namespace USB +} // namespace OHOS +#endif // USB_PORT_H diff --git a/interfaces/innerkits/native/include/usb_port_status.h b/interfaces/innerkits/native/include/usb_port_status.h new file mode 100644 index 00000000..0820da74 --- /dev/null +++ b/interfaces/innerkits/native/include/usb_port_status.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_PORT_STATUS_H +#define USB_PORT_STATUS_H + +#include "usb_common.h" +#include + +namespace OHOS { +namespace USB { +// class UsbPortStatus { +// public: +// UsbPortStatus(uint32_t currentMode, uint32_t currentPowerRole, +// uint32_t currentDataRole) { +// mCurrentMode = currentMode; +// mCurrentPowerRole = currentPowerRole; +// mCurrentDataRole = currentDataRole; +// } +// UsbPortStatus() { +// mCurrentMode = 0; +// mCurrentPowerRole = 0; +// mCurrentDataRole = 0; +// } +// ~UsbPortStatus(); + +// int32_t GetCurrentMode() const { return mCurrentMode; } + +// int32_t GetCurrentPowerRole() const { return mCurrentPowerRole; } + +// int32_t GetCurrentDataRole() const { return mCurrentDataRole; } + +// private: +// int32_t mCurrentMode; +// int32_t mCurrentPowerRole; +// int32_t mCurrentDataRole; +// }; +} // namespace USB +} // namespace OHOS + +#endif // USB_PORT_STATUS_H diff --git a/interfaces/innerkits/native/include/usb_request.h b/interfaces/innerkits/native/include/usb_request.h new file mode 100644 index 00000000..0d196756 --- /dev/null +++ b/interfaces/innerkits/native/include/usb_request.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_REQUEST_H +#define USB_REQUEST_H + +#include +#include "iusb_srv.h" +#include "securec.h" +#include "usb_endpoint.h" +#include "usb_pipe.h" + +namespace OHOS { +namespace USB { +class UsbSrvClient; +class UsbRequest { +public: + UsbRequest(); + int32_t Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint); + int32_t Queue(uint8_t *buffer, uint32_t length); + int32_t Free(); + int32_t Abort(); + void SetPipe(const USBDevicePipe &pipe) + { + this->pipe = pipe; + } + void SetEndpoint(const USBEndpoint &endpoint) + { + this->endpoint = endpoint; + } + void SetClientData(void *obj, uint32_t objLength) + { + clientData = obj; + clientLength = objLength; + } + void SetBuffer(uint8_t *buffer) { this->buffer = buffer; } + void SetBufferData(uint8_t *data, uint32_t dataSize) + { + if(data == nullptr || dataSize == 0 || length < dataSize) { + return; + } + memcpy_s(buffer, length, data, dataSize); + } + void SetLength(uint8_t length) { this->length = length; } + void *GetClientData() { return clientData; } + const USBDevicePipe &GetPipe() const { return pipe; } + const USBEndpoint &GetEndpoint() const { return endpoint; } + uint32_t GetClientLength() const { return clientLength; } + uint8_t *GetBuffer() const { return buffer; } + uint32_t GetLength() const { return length; } + +public: + USBDevicePipe pipe; + USBEndpoint endpoint; + void *clientData; + uint32_t clientLength; + uint8_t *buffer; + uint32_t length; + +private: + UsbSrvClient *usbClient; +}; +} // namespace USB +} // namespace OHOS + +#endif // USB_REQUEST_H diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h new file mode 100644 index 00000000..1b975a39 --- /dev/null +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBMGR_USB_SRV_CLIENT_H +#define USBMGR_USB_SRV_CLIENT_H + +#include +#include +#include +#include +#include "iremote_object.h" +#include "iusb_srv.h" +#include "usb_device.h" +#include "usb_pipe.h" +#include "usb_port.h" +#include "usb_request.h" + +namespace OHOS { +namespace USB { + +const std::string MAXVERSION = "001"; +const std::string SUBVERSION = "001"; +const std::string DLPVERSION = "006"; +const std::string SEVVERSION = MAXVERSION + "." + SUBVERSION + "." + DLPVERSION; + +class UsbSrvClient final : public DelayedRefSingleton { + + +DECLARE_DELAYED_REF_SINGLETON(UsbSrvClient) + +public: + DISALLOW_COPY_AND_MOVE(UsbSrvClient); + + int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pip); + int32_t HasRight(std::string deviceName); + int32_t RequestRight(std::string deviceName); + int32_t GetDevice(uint8_t busNum, uint8_t devAddr, UsbDevice &devInfo); + int32_t GetDevices(std::vector &deviceList); + int32_t GetPorts(std::vector &usbports); + int32_t GetSupportedModes(int32_t portId); + int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole); + int32_t GetCurrentFunctions(); + bool SetCurrentFunctions(int32_t funcs); + int32_t UsbFunctionsFromString(std::string funcs); + std::string UsbFunctionsToString(int32_t funcs); + int32_t ClaimInterface(USBDevicePipe &pip, const UsbInterface &interface, bool force); + int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface); + int32_t BulkTransfer(USBDevicePipe &pip, const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout); + int32_t ControlTransfer(USBDevicePipe &pip, int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout); + int32_t SetConfiguration(USBDevicePipe &pip, const USBConfig &config); + int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface); + int32_t GetRawDescriptors(uint8_t *buffer, uint32_t length); + int32_t GetFileDescriptor(); + bool Close(const USBDevicePipe &pip); + int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeout, UsbRequest &req); + + int32_t RequestInitialize(UsbRequest &request); + int32_t RequestFree(UsbRequest &request); + int32_t RequestAbort(UsbRequest &request); + int32_t RequestQueue(UsbRequest &request, uint8_t *buffer, uint32_t length); + + std::string GetVersion(){ + return SEVVERSION; + } + +private: + class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { + public: + UsbSrvDeathRecipient() = default; + ~UsbSrvDeathRecipient() = default; + void OnRemoteDied(const wptr &remote); + + private: + DISALLOW_COPY_AND_MOVE(UsbSrvDeathRecipient); + }; + + static void PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length); + int32_t Connect(); + sptr proxy_{nullptr}; + sptr deathRecipient_{nullptr}; + void ResetProxy(const wptr &remote); + std::mutex mutex_; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USBMGR_USB_SRV_CLIENT_H diff --git a/interfaces/innerkits/native/src/usb_device.cpp b/interfaces/innerkits/native/src/usb_device.cpp new file mode 100644 index 00000000..96ffc63b --- /dev/null +++ b/interfaces/innerkits/native/src/usb_device.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_device.h" + +namespace OHOS +{ + namespace USB + { +#if 0 + UsbDevice::UsbDevice() + { + mVendorId = 0; + mProductId = 0; + mClass = 0; + mSubclass = 0; + mProtocol = 0; + } + + UsbDevice::~UsbDevice() + { + } +#endif + //UsbDevice * UsbDevice::build( IUsbSerialReader serialReader) { + // return nullptr; + //} + + } // namespace USB +} // namespace OHOS + diff --git a/interfaces/innerkits/native/src/usb_pipe.cpp b/interfaces/innerkits/native/src/usb_pipe.cpp new file mode 100644 index 00000000..f9584ee1 --- /dev/null +++ b/interfaces/innerkits/native/src/usb_pipe.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_pipe.h" +#include "usb_device.h" +#include "usb_request.h" +#include "iusb_srv.h" +#include "usb_common.h" +#include "usb_srv_client.h" + +namespace OHOS +{ + namespace USB + { + USBDevicePipe::USBDevicePipe() + : usbClient(&UsbSrvClient::GetInstance()) { + } + USBDevicePipe::USBDevicePipe(uint8_t busNum, uint8_t devAddr, int32_t fd) + : busNum(busNum) + , devAddr(devAddr) + , fd(fd) + , usbClient(&UsbSrvClient::GetInstance()) { + } + + int32_t USBDevicePipe::Close() { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + int32_t ret = ERR_OK; + bool bSuccess = usbClient->Close(*this); + if(!bSuccess) { + ret = ERR_INVALID_VALUE; + } + return ret; + } + int32_t USBDevicePipe::ClaimInterface(const UsbInterface &interface, bool force) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + return usbClient->ClaimInterface(*this, interface, force); + } + int32_t USBDevicePipe::ReleaseInterface(const UsbInterface &interface) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + return usbClient->ReleaseInterface(*this, interface); + } + int32_t USBDevicePipe::BulkTransfer(const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + return usbClient->BulkTransfer(*this, endpoint, buffer, length, timeout); + } + int32_t USBDevicePipe::ControlTransfer(int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + return usbClient->ControlTransfer(*this, requestType, request, value, index, buffer, length, timeout); + } + int32_t USBDevicePipe::SetConfiguration(const USBConfig &config) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + return usbClient->SetConfiguration(*this, config); + } + int32_t USBDevicePipe::SetInterface(const UsbInterface &interface) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + return usbClient->SetInterface(*this, interface); + } + int32_t USBDevicePipe::GetRawDescriptors(uint8_t *buffer, uint32_t &length) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + //return proxy_->GetRawDescriptor(busNum, devAddr, buffer, length); + return ERR_OK; + } + int32_t USBDevicePipe::GetFileDescriptor() const + { + return fd; + } + int32_t USBDevicePipe::RequestWait(int32_t timeout, UsbRequest &req) + { + RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT); + return usbClient->PipeRequestWait(*this, timeout, req); + } + void USBDevicePipe::SetBusNum(uint8_t busNum) { + this->busNum = busNum; + } + void USBDevicePipe::SetDevAddr(uint8_t devAddr) { + this->devAddr = devAddr; + } + void USBDevicePipe::SetFd(int32_t fd) { + this->fd = fd; + } + void USBDevicePipe::SetSerial(const std::string &serial) { + this->serial = serial; + } + uint8_t USBDevicePipe::GetBusNum() const { + return busNum; + } + uint8_t USBDevicePipe::GetDevAddr() const { + return devAddr; + } + uint8_t USBDevicePipe::GetFd() const { + return fd; + } + std::string USBDevicePipe::GetSerial() const { + return serial; + } + } // namespace USB +} // namespace OHOS + diff --git a/interfaces/innerkits/native/src/usb_request.cpp b/interfaces/innerkits/native/src/usb_request.cpp new file mode 100644 index 00000000..ba7d6166 --- /dev/null +++ b/interfaces/innerkits/native/src/usb_request.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_request.h" +#include "usb_common.h" +#include "usb_srv_client.h" + +namespace OHOS { +namespace USB { + UsbRequest::UsbRequest() + : clientData(nullptr) + , clientLength(0) + , buffer(nullptr) + , length(0) + , usbClient(&UsbSrvClient::GetInstance()) + { + } + int32_t UsbRequest::Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint) { + this->pipe = pipe; + this->endpoint = endpoint; + return usbClient->RequestInitialize(*this); + } + + int32_t UsbRequest::Queue(uint8_t *buffer, uint32_t length) + { + this->buffer = buffer; + this->length = length; + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : buffer=%{public}p, length=%{public}d", __func__, buffer, length); + return usbClient->RequestQueue(*this, buffer, length); + } + + int32_t UsbRequest::Free() + { + return usbClient->RequestFree(*this); + } + + int32_t UsbRequest::Abort() + { + return usbClient->RequestAbort(*this); + } +} // namespace USB +} // namespace OHOS + diff --git a/interfaces/innerkits/native/src/usb_srv_client.cpp b/interfaces/innerkits/native/src/usb_srv_client.cpp new file mode 100644 index 00000000..aef5dafb --- /dev/null +++ b/interfaces/innerkits/native/src/usb_srv_client.cpp @@ -0,0 +1,376 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_srv_client.h" +#include +#include "datetime_ex.h" +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "usb_common.h" + +#include "system_ability_definition.h" +#include "usb_common.h" +#include "usb_device.h" + +namespace OHOS { +namespace USB { +#define USB_MAX_REQUEST_DATA_SIZE 1024 + +UsbSrvClient::UsbSrvClient() { + Connect(); +} +UsbSrvClient::~UsbSrvClient() {} + +void UsbSrvClient::PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length) +{ + std::ostringstream oss; + if(title == NULL || buffer == nullptr || length == 0) { + return; + } + oss.str(""); + oss << title << " << 二进制数据流[" << length << "字节] >> :" ; + for(uint32_t i = 0; i < length; ++i) { + oss << " " << std::hex << (int)buffer[i]; + } + oss << " --> " << buffer << std::endl; + USB_HILOGD(MODULE_USB_INNERKIT, "%{public}s", oss.str().c_str()); +} +int32_t UsbSrvClient::Connect() { + std::lock_guard lock(mutex_); + if (proxy_ != nullptr) { + return ERR_OK; + } + sptr sm = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:fail to get Registry", __func__); + return E_GET_SYSTEM_ABILITY_MANAGER_FAILED; + } + sptr remoteObject_ = sm->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + if (remoteObject_ == nullptr) { + USB_HILOGE(MODULE_USB_INNERKIT, "GetSystemAbility failed."); + return E_GET_USB_SERVICE_FAILED; + } + proxy_ = iface_cast(remoteObject_); + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s :Connect UsbService ok.", __func__); + return ERR_OK; +} + +void UsbSrvClient::ResetProxy(const wptr &remote) { + std::lock_guard lock(mutex_); + RETURN_IF(proxy_ == nullptr); + auto serviceRemote = proxy_->AsObject(); + if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { + serviceRemote->RemoveDeathRecipient(deathRecipient_); + proxy_ = nullptr; + } +} + +void UsbSrvClient::UsbSrvDeathRecipient::OnRemoteDied( + const wptr &remote) { + if (remote == nullptr) { + USB_HILOGE( + MODULE_USB_INNERKIT, + "UsbSrvDeathRecipient::OnRemoteDied failed, remote is nullptr."); + return; + } + UsbSrvClient::GetInstance().ResetProxy(remote); + USB_HILOGI(MODULE_USB_INNERKIT, "UsbSrvDeathRecipient::Recv death notice."); +} + +int32_t UsbSrvClient::OpenDevice(const UsbDevice &device, USBDevicePipe &pipe) +{ + int32_t ret; + USB_HILOGI(MODULE_USB_INNERKIT, " Calling OpenDevice Start!"); + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + //RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_NO_INIT); + ret = proxy_->OpenDevice(device.GetBusNum(), device.GetDevAddr()); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret); + return ret; + } + + pipe.SetBusNum(device.GetBusNum()); + pipe.SetDevAddr(device.GetDevAddr()); + //pipe.SetFd(fd); + return ERR_OK; +} + +int32_t UsbSrvClient::HasRight(std::string deviceName) { + int32_t ret = 0; + USB_HILOGI(MODULE_USB_INNERKIT, " Calling HasRight Start!"); + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + ret = proxy_->HasRight(deviceName); + if (ret != 0) { + USB_HILOGI(MODULE_USB_INNERKIT, " Calling HasRight Success!"); + } + return ret; +} + +int32_t UsbSrvClient::RequestRight(std::string deviceName) { + int32_t ret = 0; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + ret = proxy_->RequestRight(deviceName); + if (ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, " Calling RequestRight Success!"); + } + USB_HILOGE(MODULE_USB_INNERKIT, " Calling RequestRight False!"); + return ret; +} + +int32_t UsbSrvClient::GetDevices(std::vector &deviceList) +{ + int32_t ret; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + ret = proxy_->GetDevices(deviceList); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret); + } + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s list size = %{public}d!", __func__, deviceList.size()); + return ret; +} + +int32_t UsbSrvClient::GetDevice(uint8_t busNum, uint8_t devAddr, UsbDevice &devInfo) +{ + int32_t ret = 0; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ret); + ret = proxy_->GetDevice(busNum, devAddr, devInfo); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret); + } + return ret; +} + +int32_t UsbSrvClient::GetCurrentFunctions() { + int32_t ret = 0; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + ret = proxy_->GetCurrentFunctions(); + if(ret < ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret); + } + USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetCurrentFunctions Success!"); + return ret; +} +bool UsbSrvClient::SetCurrentFunctions(int32_t funcs) { + RETURN_IF_WITH_RET(Connect() != ERR_OK, false); + bool ret = proxy_->SetCurrentFunctions(funcs); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret); + return false; + } + USB_HILOGI(MODULE_USB_INNERKIT, " Calling SetCurrentFunctions Success!"); + return true; +} + +int32_t UsbSrvClient::UsbFunctionsFromString(std::string funcs) { + int32_t result = 0; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + result = proxy_->UsbFunctionsFromString(funcs); + USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsFromString Success!"); + return result; +} + +std::string UsbSrvClient::UsbFunctionsToString(int32_t funcs) { + std::string result; + RETURN_IF_WITH_RET(Connect() != ERR_OK, result); + result = proxy_->UsbFunctionsToString(funcs); + USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsToString Success!"); + return result; +} + +int32_t UsbSrvClient::GetPorts(std::vector & usbports) { + int32_t ret = 0; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetPorts Success!"); + ret = proxy_->GetPorts(usbports); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret); + } + return ret; +} + +int32_t UsbSrvClient::GetSupportedModes(int32_t portId) { + int32_t ret = 0; + int32_t result = 0; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetSupportedModes Success!"); + ret = proxy_->GetSupportedModes(portId, result); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret); + } + return result; +} + +int32_t UsbSrvClient::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) { + int32_t ret = 0; + RETURN_IF_WITH_RET(Connect() != ERR_OK, ERR_NO_INIT); + USB_HILOGI(MODULE_USB_INNERKIT, " Calling SetPortRole Success!"); + ret = proxy_->SetPortRole(portId, powerRole, dataRole); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret); + } + return ret; +} + +int32_t UsbSrvClient::ClaimInterface(USBDevicePipe &pipe, const UsbInterface &interface, bool force) +{ + int32_t ret = ERR_INVALID_VALUE; + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_NO_INIT); + ret = proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId()); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret); + } + return ret; +} +int32_t UsbSrvClient::ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &interface) +{ + int32_t ret = ERR_INVALID_VALUE; + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_NO_INIT); + ret = proxy_->ReleaseInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId()); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret); + } + return ret; +} +int32_t UsbSrvClient::BulkTransfer(USBDevicePipe &pipe, const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout) +{ + int32_t ret = ERR_INVALID_VALUE; + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_NO_INIT); + if(USB_ENDPOINT_DIR_IN == endpoint.GetDirection()) { + ret = proxy_->BulkTransferRead(pipe.GetBusNum(), pipe.GetDevAddr(), endpoint.GetInterfaceId(), endpoint.GetAddress(), buffer, length, timeout); + } else if(USB_ENDPOINT_DIR_OUT == endpoint.GetDirection()) { + ret = proxy_->BulkTransferWrite(pipe.GetBusNum(), pipe.GetDevAddr(), endpoint.GetInterfaceId(), endpoint.GetAddress(), buffer, length, timeout); + } + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret); + } + return ret; +} +int32_t UsbSrvClient::ControlTransfer(USBDevicePipe &pipe, int32_t requestType, int32_t request, int32_t value, int32_t index, + uint8_t *buffer, uint32_t &length, int32_t timeout) +{ + int32_t ret = ERR_INVALID_VALUE; + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_NO_INIT); + ret = proxy_->ControlTransfer(pipe.GetBusNum(), pipe.GetDevAddr(), requestType, request, value, index, buffer, length, timeout); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret); + } + + return ret; +} +int32_t UsbSrvClient::SetConfiguration(USBDevicePipe &pipe, const USBConfig &config) +{ + int32_t ret = ERR_OK; + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_NO_INIT); + ret = proxy_->SetActiveConfig(pipe.GetBusNum(), pipe.GetDevAddr(), config.GetId()); + return ret; +} +int32_t UsbSrvClient::SetInterface(USBDevicePipe &pipe, const UsbInterface &interface) +{ + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_NO_INIT); + return proxy_->SetInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), interface.GetAlternateSetting()); +} +int32_t UsbSrvClient::GetRawDescriptors(uint8_t *buffer, uint32_t length) { length=0; return 0;} +int32_t UsbSrvClient::GetFileDescriptor() {return 0;} + +bool UsbSrvClient::Close(const USBDevicePipe &pipe) +{ + int32_t ret = ERR_OK; + RETURN_IF_WITH_RET(proxy_ == nullptr, false); + ret = proxy_->Close(pipe.GetBusNum(), pipe.GetDevAddr()); + return (ret == ERR_OK); +} + +int32_t UsbSrvClient::PipeRequestWait(USBDevicePipe &pipe, int64_t timeout, UsbRequest &req) +{ + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_INVALID_VALUE); + int32_t ret = ERR_OK; + uint32_t length = USB_MAX_REQUEST_DATA_SIZE; + uint8_t *buffer = new uint8_t[length]; + if(buffer == nullptr) { + return ERR_NO_MEMORY; + } + uint32_t clientLength = sizeof(UsbRequest); + uint8_t *clientObj = new uint8_t[clientLength]; + //PrintBuffer("UsbSrvClient::PipeRequestWait in", (const uint8_t*)clientObj, clientLength); + ret = proxy_->RequestWait(pipe.GetBusNum(), pipe.GetDevAddr(), timeout, clientObj, clientLength, buffer, length); + if(ret != ERR_OK) { + delete [] buffer; + delete [] clientObj; + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s :failed width ret = %{public}d.", __func__, ret); + return ret; + } + + PrintBuffer("UsbSrvClient::PipeRequestWait out", (const uint8_t*)clientObj, clientLength); + UsbRequest *sendReq = reinterpret_cast(clientObj); + if(sendReq == nullptr || clientLength == 0) { + delete [] buffer; + delete [] clientObj; + return ERR_INVALID_VALUE; + } + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : buffer=%{public}p, length=%{public}d", __func__, sendReq->GetBuffer(), sendReq->GetLength()); + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : clientData=%{public}p, ClientLength=%{public}d", __func__, sendReq->GetClientData(), sendReq->GetClientLength()); + + req = *sendReq; + if(length > 0) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : buffer=%{public}p, length=%{public}d.", __func__, req.GetBuffer(), req.GetLength()); + // req.SetBufferData(buffer, length); + req.SetLength(length); + req.SetBuffer(buffer); + } else { + delete [] buffer; + } + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s : ok", __func__); + // delete [] buffer; + delete [] clientObj; + return ERR_OK; +} + +int32_t UsbSrvClient::RequestInitialize(UsbRequest &request) +{ + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_INVALID_VALUE); + const USBDevicePipe &pipe = request.GetPipe(); + const USBEndpoint &endpoint = request.GetEndpoint(); + return proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), endpoint.GetInterfaceId()); +} + +int32_t UsbSrvClient::RequestFree(UsbRequest &request) +{ + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_INVALID_VALUE); + const USBDevicePipe &pipe = request.GetPipe(); + const USBEndpoint &ep = request.GetEndpoint(); + return proxy_->ReleaseInterface(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId()); +} + +int32_t UsbSrvClient::RequestAbort(UsbRequest &request) +{ + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_INVALID_VALUE); + const USBDevicePipe &pipe = request.GetPipe(); + const USBEndpoint &ep = request.GetEndpoint(); + return proxy_->RequestCancel(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress()); +} + +int32_t UsbSrvClient::RequestQueue(UsbRequest &request, uint8_t *buffer, uint32_t length) +{ + RETURN_IF_WITH_RET(proxy_ == nullptr, ERR_INVALID_VALUE); + const USBDevicePipe &pipe = request.GetPipe(); + const USBEndpoint &ep = request.GetEndpoint(); + return proxy_->RequestQueue(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress(), + &request, sizeof(UsbRequest), const_cast(buffer), length); +} +} // namespace USB +} // namespace OHOS diff --git a/interfaces/kits/java/BUILD.gn b/interfaces/kits/java/BUILD.gn new file mode 100644 index 00000000..f3a90d8c --- /dev/null +++ b/interfaces/kits/java/BUILD.gn @@ -0,0 +1,54 @@ +# Copyright (C) 2021 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. + +import("//build/config/ohos/rules.gni") +import("//build/ohos.gni") + +USB_MANAGER_ROOT = "//base/usb/usb_manager" +ADAPTER_DIR = "$USB_MANAGER_ROOT/adapter" +INTERFACES_ROOT = "$USB_MANAGER_ROOT/interfaces" + +java_library("usb_manager_java") { + java_files = [ + "$INTERFACES_ROOT/kits/java/ohos/usb/USBLoggerFactory.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/USBDevice.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/USBDevicePipe.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/USBInterface.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/USBCore.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/USBRequest.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/USBConfig.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/USBEndpoint.java", + "$INTERFACES_ROOT/kits/java/ohos/usb/package-info.java", + ] + + deps = [ "//utils/java:utils_maple_java" ] + + external_deps = [ + "appexecfwk:appexecfwk_java", + "appexecfwk:appexecfwk_java_maple", + "hilog:hilog_maple_java", + "samgr:samgr_java", + "samgr:samgr_java_maple", + ] + + subsystem_name = "usb" + part_name = "usb_manager" +} + +ohos_maple_java("usb_manager_maple_java") { + deps = [ ":usb_manager_java" ] + external_deps = [ "hilog:hilog_maple_java" ] + so_name = "usb_manager_java" + part_name = "usb_manager" + output_dir = "$root_build_dir/usb/" +} diff --git a/interfaces/kits/java/ohos/usb/IUSBDevicePipeController.java b/interfaces/kits/java/ohos/usb/IUSBDevicePipeController.java new file mode 100644 index 00000000..50adf081 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/IUSBDevicePipeController.java @@ -0,0 +1,42 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.event.intentagent.IntentAgent; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.RemoteException; +import ohos.rpc.IRemoteObject; +import ohos.interwork.utils.PacMapEx; + +public interface IUSBDevicePipeController extends IRemoteBroker { + + int bulkTransfer(USBEndpoint epObj, byte[] buffer, int length, int timeout) throws RemoteException; + + int bulkTransfer(USBEndpoint epObj, byte[] buffer, int offset, int length, int timeout) throws RemoteException; + + boolean claimInterface(USBInterface interfaceObj, boolean force) throws RemoteException; + + void close() throws RemoteException; + + int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int offset, int length, int timeout) throws RemoteException; + + int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout) throws RemoteException; + + int getFileDescriptor() throws RemoteException; + + byte[] getRawDescriptors() throws RemoteException; + + String getSerial() throws RemoteException; + + boolean releaseInterface(USBInterface interfaceObj) throws RemoteException; + + USBRequest requestWait(long timeout) throws TimeoutException, RemoteException; + + USBRequest requestWait() throws RemoteException; + + boolean setConfiguration(USBConfig config) throws RemoteException; + + boolean setInterface(USBInterface interfaceObj) throws RemoteException; + + boolean openByInt(String name, int fd) throws RemoteException; + +} diff --git a/interfaces/kits/java/ohos/usb/IUSBRequestController.java b/interfaces/kits/java/ohos/usb/IUSBRequestController.java new file mode 100644 index 00000000..990baa03 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/IUSBRequestController.java @@ -0,0 +1,28 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.event.intentagent.IntentAgent; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.RemoteException; +import ohos.rpc.IRemoteObject; +import ohos.interwork.utils.PacMapEx; + +import java.util.List; + +public interface IUsbRequestController extends IRemoteBroker { + + void abort() throws RemoteException; + + void free() throws RemoteException; + + boolean initialize(USBDevicePipe connection, USBEndpoint ep) throws RemoteException; + + boolean queue(ByteBuffer buffer) throws RemoteException; + + boolean queue(ByteBuffer buffer, int length) throws RemoteException; + + void dequeue(boolean useBufferOverflowInsteadOfIllegalArg) throws RemoteException; + + void setClientData(Object data) throws RemoteException; + +} \ No newline at end of file diff --git a/interfaces/kits/java/ohos/usb/IUsbCoreController.java b/interfaces/kits/java/ohos/usb/IUsbCoreController.java new file mode 100644 index 00000000..13d34442 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/IUsbCoreController.java @@ -0,0 +1,43 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.event.intentagent.IntentAgent; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.RemoteException; +import ohos.rpc.IRemoteObject; +import ohos.interwork.utils.PacMapEx; + +import java.util.List; + +public interface IUsbCoreController extends IRemoteBroker { + + + HashMap getDevices() throws RemoteException; + + USBDevicePipe connectDevice(USBDevice device) throws RemoteException; + + boolean hasRight(USBDevice device) throws RemoteException; + + void requestRight(USBDevice device, String intentName) throws RemoteException; + + String usbFunctionsToString(long functions) throws RemoteException; + + long usbFunctionsFromString(String functions) throws RemoteException; + + String getCurrentFunctions() throws RemoteException; + + void setCurrentFunctions(long functions) throws RemoteException; + + void setCurrentFunction(String functions, boolean usbDataUnlocked) throws RemoteException; + + void setHdcEnabled(boolean enable) throws RemoteException; + + void addFunction(String functions, String function) throws RemoteException; + + void removeFunction(String functions, String function) throws RemoteException; + + void setPortRoles(int powerRole,int dataRole) throws RemoteException; + + UsbPortStatus getPortStatus(USBPort port) throws RemoteException; + +} diff --git a/interfaces/kits/java/ohos/usb/USBConfig.java b/interfaces/kits/java/ohos/usb/USBConfig.java new file mode 100644 index 00000000..4d43cad6 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBConfig.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import ohos.utils.system.SystemCap; +import ohos.utils.system.SystemCapability; + +/** + * Gets information about USB configuration. + * + *

When using this class, a USB device is attached to a USB host. According to the USB protocol specifications, + * a device contains one or more configurations, a configuration contains one or more interfaces, and an interface + * contains one or more endpoints. + * + * @Syscap {@link SystemCapability.Usb#USB_MANAGER} + * @since 5 + */ +@SystemCap(SystemCapability.Usb.USB_MANAGER) +public class USBConfig { + private int id; + + private int maxPower; + + private String name; + + private boolean isRemoteWakeup; + + private boolean isSelfPowered; + + private USBInterface[] interfaces; + + public USBConfig(int id, int maxPower, String name, boolean isRemoteWakeup, boolean isSelfPowered) { + this.id = id; + this.maxPower = maxPower; + this.name = name; + this.isRemoteWakeup = isRemoteWakeup; + this.isSelfPowered = isSelfPowered; + } + + /** + * Get a {@code USBInterface} object by index in the {@code USBConfig}. + * + * @param index Indicates the {@code USBInterface} object index in the {@code USBConfig}. + * @return Returns the {@code USBInterface} object . + * @since 5 + */ + public USBInterface getInterface(int index) { + if (interfaces == null || index < 0 || index >= interfaces.length) { + return null; + } + + return interfaces[index]; + } + + /** + * Sets a local {@code USBInterface} object. + * + * @param interfaces Indicates a local {@code USBInterface} object. + * @since 5 + */ + public void setInterfaces(USBInterface[] interfaces) { + this.interfaces = interfaces; + } + + /** + * Get the number of interfaces of the configuration. + * + * @return Returns the number of interfaces of the configuration. + * @since 5 + */ + public int getInterfaceCount() { + if (interfaces == null) { + return 0; + } + + return interfaces.length; + } + + /** + * Get the current configuration ID. + * + * @return Returns the current configuration ID. + * @since 5 + */ + public int getId() { + return id; + } + + /** + * Get the maximum power consumption of the current configuration. + * + * @return Returns the maximum power consumption. + * @since 5 + */ + public int getMaxPower() { + return maxPower; + } + + /** + * Get the current configuration name. + * + * @return Returns the current configuration name. + * @since 5 + */ + public String getName() { + return name; + } + + /** + * Checks whether the current configuration supports remote wakeup. + * + * @return Returns {@code true} if the configuration supports remote wakeup; returns {@code false} otherwise. + * @since 5 + */ + public boolean isRemoteWakeup() { + return isRemoteWakeup; + } + + /** + * Checks whether the configuration supports an independent power source. + * + * @return Returns {@code true} if the configuration supports an independent power source; + * returns {@code false} otherwise. + * @since 5 + */ + public boolean isSelfPowered() { + return isSelfPowered; + } +} + diff --git a/interfaces/kits/java/ohos/usb/USBCore.java b/interfaces/kits/java/ohos/usb/USBCore.java new file mode 100644 index 00000000..2b9a5dbf --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBCore.java @@ -0,0 +1,191 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.StringJoiner; + +import ohos.app.Context; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.sysability.samgr.SysAbilityManager; +import ohos.utils.system.SystemCap; +import ohos.utils.system.SystemCapability; + +/** + * This class allows you to obtain USB devices currently attached, obtain USB device status, + * and communicate with USB devices. + * Currently, only the host mode is supported. + * + * @Syscap {@link SystemCapability.Usb#USB_MANAGER} + * @since 5 + */ +@SystemCap(SystemCapability.Usb.USB_MANAGER) +public class USBCore { + + /** + * A constructor used to create a {@code USBCore}. + * + * @since 5 + */ + public USBCore() { + + } + + /** + * Returns the hash map of USB devices. + * + *

The hash map contains devices and their names. Applications can traverse USB devices in the hash map to + * perform operations on the devices. For details on the operations, see {@link ohos.usb.USBDevice}. + * + * @return Returns a hash map containing devices and their names. + * @since 5 + */ + public HashMap getDevices() { + HashMap results = new HashMap(); + native_get_devices(results); + return results; + } + + public HashMap getPeripherals() { + + } + + /** + * Opens a USB device based on device information returned by {@link ohos.usb.USBCore#getDevices}. + * + *

A {@link ohos.usb.USBDevicePipe} instance is returned. Data transfer is performed based on the + * instance. + * + * @param device Indicates a {@link ohos.usb.USBDevicePipe} instance. + * @return Returns a {@link ohos.usb.USBDevicePipe} instance. + * @since 5 + */ + public USBDevicePipe connectDevice(USBDevice device) { + return native_connnect_device(device); + } + + /** + * Returns true if the caller has right to access the device. + * + * @param device to check right for + * @return {@code true} if caller has right to access this device + * @since 5 + */ + public boolean hasRight(USBDevice device) { + return native_has_right(device); + } + + /** + * Requests temporary permission for the given package to access the device. + * + * @param device Device to request permissions for + * @param intentName Intent for returning result + */ + public void requestRight(USBDevice device, String intentName) { + native_request_right(device, intentName); + } + + /** + * Converts the given function mask to string. Maintains ordering with respect to init scripts. + * + * @param functions the usb functions long value + * @return String representation of given mask + * @since 5 + */ + public String usbFunctionsToString(long functions) { + return native_usb_function_to_string(functions); + } + + /** + * Parses a string of usb functions that are comma separated. + * + * @param functions the usb functions + * @return A mask of all valid functions in the string + * @since 5 + */ + public long usbFunctionsFromString(String functions) { + return native_usb_function_from_string(functions); + } + + public String getCurrentFunctions() { + return native_get_current_functions(); + } + + public void setCurrentFunctions(long functions) { + native_set_current_functions(functions); + } + + public void setCurrentFunction(String functions, boolean usbDataUnlocked) { + native_set_current_function(functions, usbDataUnlocked); + } + + public void setHdcEnabled(boolean enable) { + native_set_hdc_enabled(enable); + } + + public void addFunction(String functions, String function) { + native_add_function(functions, function); + } + + public void removeFunction(String functions, String function) { + native_remove_function(functions, function); + } + + public void setPortRoles(int powerRole,int dataRole) { + native_set_port_roles(powerRole, dataRole); + } + + public UsbPortStatus getPortStatus(USBPort port) { + return native_get_port_status(port); + } + + private native void native_get_devices(HashMap results); + + private native USBDevicePipe native_connnect_device(USBDevice device); + + private native boolean native_has_right(USBDevice device); + + private native void native_request_right(USBDevice device, String intentName); + + private native String native_usb_function_to_string(long functions); + + private native long native_usb_function_from_string(String functions); + + private native String native_get_current_functions(); + + private native void native_set_current_functions(long functions); + + private native void native_set_current_function(String functions, boolean usbDataUnlocked); + + private native void native_set_hdc_enabled(boolean enable); + + private native void native_add_function(String functions, String function); + + private native void native_remove_function(String functions, String function); + + private native void native_set_port_roles(int powerRole,int dataRole); + + private native UsbPortStatus native_get_port_status(USBPort port); +} + + + + + + diff --git a/interfaces/kits/java/ohos/usb/USBDevice.java b/interfaces/kits/java/ohos/usb/USBDevice.java new file mode 100644 index 00000000..428c24e7 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBDevice.java @@ -0,0 +1,290 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import ohos.utils.system.SystemCap; +import ohos.utils.system.SystemCapability; + +/** + * Get configuration information of a USB device. For details on the information + * structure, see USB protocol specifications. + * + *

+ * When using this class, a USB device is attached to a USB host. According to + * the USB protocol specifications, a device contains one or more + * configurations, a configuration contains one or more interfaces, and an + * interface contains one or more endpoints. + * + * @Syscap {@link SystemCapability.Usb#USB_MANAGER} + * @since 5 + */ +@SystemCap(SystemCapability.Usb.USB_MANAGER) +public class USBDevice { + + private final int deviceId; + + private final String deviceName; + + private final String manufacturerName; + + private final String productName; + + private final String version; + + private final String serialNumber; + + private final int vendorId; + + private final int productId; + + private final int classId; + + private final int subclass; + + private final int protocol; + + private USBConfig[] configs; + + private USBInterface[] interfaces; + + /** + * A constructor used to create a {@code USBDevice}. + * + * @param deviceId Indicates the id of the USB device, which is usually determined by the low layer. + * @param deviceName Indicates the name of the USB device, which is usually determined by the low layer. + * @param vendorId Indicates the vendor ID of the USB device, which is assigned by the USB-IF. + * @param productId Indicates the product ID of the USB device, which is assigned by the USB-IF. + * @param classId Indicates the class code of the USB device, which is assigned by the USB-IF. + * @param subClassId Indicates the subclass code of the USB device, which is assigned by the USB-IF. + * @param protocolId Indicates the protocol code of the USB device, which is assigned by the USB-IF. + * @param manufacturerName Indicates the manufacturer name of the USB device. + * @param productName Indicates the product name of the USB device. + * @param version Indicates the version number of the USB device. + * @param serialNumber Indicates the serial number of the USB device. + * + * @since 5 + */ + public USBDevice(int deviceId, String deviceName, int vendorId, int productId, int classId, int subClassId, int protocolId, + String manufacturerName, String productName, String version, String serialNumber) { + this.deviceId = deviceId; + this.deviceName = deviceName; + this.vendorId = vendorId; + this.productId = productId; + this.classId = classId; + this.subclass = subClassId; + this.protocol = protocolId; + this.manufacturerName = manufacturerName; + this.productName = productName; + this.version = version; + this.serialNumber = serialNumber; + } + + /** + * Gets the {@code Class} field of a USB device. + * + * @return Returns the {@code Class} field. + * @since 5 + */ + public int getDeviceClass() { + return classId; + } + + /** + * Gets the {@code Id} field of a USB device. + * + * @return Returns the {@code Id} field. + * @since 5 + */ + public int getDeviceId() { + return deviceId; + } + + /** + * Get the {@code Name} field of a USB device. + * + * @return Returns the {@code Name} field. + * @since 5 + */ + public String getDeviceName() { + return deviceName; + } + + /** + * Get the {@code Name} field of a USB device. + * + * @param id Id of the USB device. + * @return Returns the {@code Name} field. + * @since 5 + */ + public static String getDeviceName(int id) { + return "default_dev"; + } + + /** + * Get the {@code Protocol} field of a USB device. + * + * @return Returns the {@code Protocol} field. + * @since 5 + */ + public int getDeviceProtocol() { + return protocol; + } + + /** + * Get the {@code SubClass} field of a USB device. + * + * @return Returns the {@code SubClass} field. + * @since 5 + */ + public int getDeviceSubclass() { + return subclass; + } + + /** + * Sets interfaces for a local object. + * + * @param interfaces Indicates the array of a {@code USBInterface} object. + * @since 5 + */ + public void setInterfaces(USBInterface[] interfaces) { + this.interfaces = interfaces; + } + + /** + * Get an interface instance of this device by index. + * + * @param index Indicates the interface index. + * @return Returns the interface. + * @since 5 + */ + public USBInterface getInterface(int index) { + if (interfaces == null || index < 0 || index >= interfaces.length) { + return null; + } + return interfaces[index]; + } + + /** + * Get the number of interfaces of this device. + * + * @return Returns the number of interfaces. + * @since 5 + */ + public int getInterfaceCount() { + if (interfaces == null) { + return 0; + } + + return interfaces.length; + } + + /** + * Get the manufacturer string of this device. + * + * @return Returns the manufacturer string of this device. + * @since 5 + */ + public String getManufacturerName() { + return manufacturerName; + } + + /** + * Get the {@code ProductId} field of a USB device. + * + * @return Returns the {@code ProductId} field. + * @since 5 + */ + public int getProductId() { + return productId; + } + + /** + * Get the {@code ProductName} field of a USB device. + * + * @return Returns the {@code ProductName} field. + * @since 5 + */ + public String getProductName() { + return productName; + } + + /** + * Get the {@code SerialNumber} field of a USB device. + * + * @return Returns the {@code SerialNumber} field. + * @since 5 + */ + public String getSerialNumber() { + return serialNumber; + } + + /** + * Get the {@code VendorId} field of a USB device. + * + * @return Returns the {@code VendorId} field. + * @since 5 + */ + public int getVendorId() { + return vendorId; + } + + /** + * Get the {@code Version} field of a USB device. + * + * @return Returns the {@code Version} field. + * @since 5 + */ + public String getVersion() { + return version; + } + + /** + * Get a {@code USBConfig} instance of this device by index. + * + * @param index Indicates the configuration index. + * @return Returns the corresponding {@code USBConfig} object. + * @since 5 + */ + public USBConfig getConfiguration(int index) { + if (configs == null || index < 0 || index >= configs.length) { + return null; + } + return configs[index]; + } + + /** + * Get the number of configurations of this device. + * + * @return Returns the number of configurations. + * @since 5 + */ + public int getConfigurationCount() { + if (configs == null) { + return 0; + } + return configs.length; + } + + /** + * Sets configurations of this device. + * + * @param configs Indicates the array of a {@code USBConfig} object. + * @since 5 + */ + public void setConfigurations(USBConfig[] configs) { + this.configs = configs; + } +} diff --git a/interfaces/kits/java/ohos/usb/USBDevicePipe.java b/interfaces/kits/java/ohos/usb/USBDevicePipe.java new file mode 100644 index 00000000..e8ebb792 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBDevicePipe.java @@ -0,0 +1,263 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import java.util.concurrent.TimeoutException; + +import ohos.utils.system.SystemCap; +import ohos.utils.system.SystemCapability; + +/** + * Provides methods for transferring data and control messages between USB devices. + * + *

Applications can get device descriptors from {@link ohos.usb.USBDevice} to operate the USB devices. + * + * @Syscap {@link SystemCapability.Usb#USB_MANAGER} + * @since 5 + */ +@SystemCap(SystemCapability.Usb.USB_MANAGER) +public class USBDevicePipe { + /** + * Indicates a {@code USBDevice}. + */ + public final USBDevice device; + + public USBDevicePipe(USBDevice device) { + this.device = device; + } + + /** + * Performs bulk data transfer on a specified endpoint. + * + * @param epObj the endpoint instance. + * @param buffer Indicates the buffer for bulk data transfer. + * @param length Indicates the length of the data to transfer. + * @param timeout Indicates the transfer timeout period, in millisecond. The value {@code 0} indicates that the + * transfer never times out. + * @return Returns a value greater than or equal to {@code 0} if the data transfer is successful; returns a + * negative value otherwise. + * @since 5 + */ + public int bulkTransfer(USBEndpoint epObj, byte[] buffer, int length, int timeout) { + return native_bulk_transfer(epObj, buffer, 0, length, timeout); + } + + /** + * Performs bulk data transfer on a specified endpoint. + * + * @param epObj the endpoint instance. + * @param buffer Indicates the buffer for bulk data transfer. + * @param offset Indicates the offset from first byte in the buffer. + * @param length Indicates the length of the data to transfer. + * @param timeout Indicates the transfer timeout period, in millisecond. The value {@code 0} indicates that the + * transfer never times out. + * @return Returns a value greater than or equal to {@code 0} if the data transfer is successful; returns a negative + * value otherwise. + * @since 5 + */ + public int bulkTransfer(USBEndpoint epObj, byte[] buffer, int offset, int length, int timeout) { + return native_bulk_transfer(epObj, buffer, offset, length, timeout); + } + + /** + * Claims that a specified interface is exclusively occupied. This must be done before transferring data to the + * endpoint of the {@code interfaceObj} interface. + * + * @param interfaceObj Indicates the interface to be exclusively occupied. + * @param force Indicates whether the kernel driver is disconnected. The value {@code true} indicates that the + * kernel driver is disconnected. The value {@code false} indicates that the kernel driver is connected + * @return Returns {@code true} if the claim is successful; returns {@code false} otherwise. + * @since 5 + */ + public boolean claimInterface(USBInterface interfaceObj, boolean force) { + return native_claim_interface(interfaceObj, force); + } + + /** + * Closes the request. + */ + public void close() { + native_close(); + } + + /** + * Performs control message transfer on endpoint zero. + * + * @param requestType Indicates the type of request initiated for control message transfer. For details, + * see USB protocol specifications. If the most significant bit is 1, data is written from the host. If the most + * significant bit is 0, data is written into the host. + * @param request Indicates the request ID. + * @param value Indicates the value of the transfer. + * @param index Indicates the index of the transfer. + * @param buffer Indicates the buffer from which data is to be transferred. + * @param offset Indicates the offset address starting from 0 in the data buffer. + * @param length Indicates the length of the data to transfer. + * @param timeout Indicates the transfer timeout period, in millisecond. + * @return Returns the number of bytes transferred; returns {@code 0} if the transfer fails. + * @since 5 + */ + public int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int offset, + int length, int timeout) { + return native_control_transfer(requestType, request, value, index, buffer, offset, length, timeout); + } + + /** + * Performs control message transfer on endpoint zero. + * + * @param requestType Indicates the type of request initiated for control message transfer. For details, see USB + * protocol specifications. If the most significant bit is 1, data is written from the host. If the most + * significant bit is 0, data is written into the host. + * @param request Indicates the request ID. + * @param value Indicates the value of the transfer. + * @param index Indicates the index of the transfer. + * @param buffer Indicates the buffer from which data is to be transferred. + * @param length Indicates the length of the data to transfer. + * @param timeout Indicates the transfer timeout period, in millisecond. + * @return Returns the number of bytes transferred; returns {@code 0} if the transfer fails. + * @since 5 + */ + public int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, + int timeout) { + return controlTransfer(requestType, request, value, index, buffer, 0, length, timeout); + } + + /** + * Get the file descriptor. + * + * @return Returns the file descriptor. + * @since 5 + */ + public int getFileDescriptor() { + return native_get_file_descriptor(); + } + + /** + * Get the original USB descriptors. + * + * @return Returns the original USB descriptors. + * @since 5 + */ + public byte[] getRawDescriptors() { + return native_get_raw_descriptors(); + } + + /** + * Get the serial number, in string. + * + * @return Returns the serial number. + * @since 5 + */ + public String getSerial() { + return native_get_serial(); + } + + /** + * Claims that a specified interface is released. + * + * @param interfaceObj Indicates the interface to release. + * @return Returns {@code true} if the release is successful; returns {@code false} otherwise. + * @since 5 + */ + public boolean releaseInterface(USBInterface interfaceObj) { + return native_release_interface(interfaceObj); + } + + /** + * Waits for the result returned by the {@link ohos.usb.USBRequest#queue} method. This is an + * asynchronous + * method call. If the operation times out, a timeout exception is thrown. + * + * @param timeout Indicates the timeout period, in milliseconds. The value {@code 0} indicates no waiting. + * @return Returns a {@link ohos.usb.USBRequest} object. + * @throws TimeoutException Throws this exception if the specified timeout elapses. + * @since 5 + */ + public USBRequest requestWait(long timeout) throws TimeoutException { + return native_request_wait(timeout); + } + + /** + * Waits for the result returned by the {@link ohos.usb.USBRequest#queue} method. + * + * @return Returns a {@link ohos.usb.USBRequest} object. + * @since 5 + */ + public USBRequest requestWait() { + return native_request_wait(); + } + + /** + * Sets the current USB configuration. + * + * @param config The config instance. + * @return Returns {@code true} if the setting is successful; returns {@code false} otherwise. + * @since 5 + */ + public boolean setConfiguration(USBConfig config) { + return native_set_configuration(config); + } + + /** + * Sets the current USB interface. + * + * @param interfaceObj Indicates a {@code USBInterface} object. + * @return Returns {@code true} if the setting is successful; returns {@code false} otherwise. + * @since 5 + */ + public boolean setInterface(USBInterface interfaceObj) { + return native_set_interface(interfaceObj); + } + + /** + * Performs an open process on the file descriptor. + * + * @param name Indicates the device name. + * @param fd Indicates the file descriptor of the device connection. + * @return Returns the result of the open process. + */ + public boolean openByInt(String name, int fd) { + return native_open_by_int(name, fd); + } + + private native int native_bulk_transfer(USBEndpoint epObj, byte[] buffer, int length, int timeout); + + private native int native_bulk_transfer(USBEndpoint epObj, byte[] buffer, int offset, int length, int timeout); + + private native boolean native_claim_interface(USBInterface interfaceObj, boolean force); + + private native void native_close(); + + private native int native_control_transfer(int requestType, int request, int value, int index, byte[] buffer, int offset, int length, int timeout); + + private native int native_get_file_descriptor(); + + private native byte[] native_get_raw_descriptors(); + + private native String native_get_serial(); + + private native boolean native_release_interface(USBInterface interfaceObj); + + private native USBRequest native_request_wait(long timeout); + + private native USBRequest native_request_wait(); + + private native boolean native_set_configuration(USBConfig config); + + private native boolean native_set_interface(USBInterface interfaceObj); + + private native boolean native_open_by_int(String name, int fd); + +} diff --git a/interfaces/kits/java/ohos/usb/USBDevicePipeController.java b/interfaces/kits/java/ohos/usb/USBDevicePipeController.java new file mode 100644 index 00000000..88904b7c --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBDevicePipeController.java @@ -0,0 +1,142 @@ +package ohos.usb; + +import java.util.concurrent.TimeoutException; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.ability.Lifecycle; +import ohos.app.Context; +import ohos.event.intentagent.IntentAgent; +import ohos.utils.Parcel; +import ohos.utils.Sequenceable; +import ohos.utils.system.SystemCap; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.rpc.MessageParcel; +import ohos.rpc.MessageOption; +import ohos.rpc.RemoteException; +import ohos.rpc.RemoteObject; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.IRemoteObject; +import ohos.interwork.utils.PacMapEx; +import ohos.nfc.tag.TagInfo; + +@SystemCap("SystemCapability.Communication.USB") +public class USBDevicePipeController { + + private static UsbDevicePipeController sUsbDevicePipeController; + private UsbDevicePipeControllerProxy mUsbDevicePipeControllerProxy; + private Context mContext; + + /** + * A callback to be invoked when the system finds a tag while the foreground page is operating in reader mode. + * + */ + public interface ReaderModeCallback extends IRemoteBroker { + /** + * Indicates the transaction code of the callback specific to inter-process communication (IPC). + */ + int TRANSACTION_ON_CALLBACK = 1; + + /** + * Called when a tag is discovered. + * + * @param tagInfo Indicates information of the discovered tag. + * @since 4 + */ + void onTagDiscovered(TagInfo tagInfo); + } + + /** + * Provides ReaderModeCallback for communicate with remote. + * + * @since 4 + */ + public class ReaderModeCallbackStub extends RemoteObject implements ReaderModeCallback { + private ReaderModeCallback callback; + + public ReaderModeCallbackStub(ReaderModeCallback callback) { + super(); + this.callback = callback; + } + + private void readInterfaceToken(Parcel data) { + data.readInt(); + data.readInt(); + data.readString(); + } + + @Override + public boolean onRemoteRequest( + int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException { + HiLog.debug(LABEL, "call ReaderModeCallbackStub's onTransact"); + + switch (code) { + case TRANSACTION_ON_CALLBACK: { + readInterfaceToken(data); + + TagInfo tagInfo = new TagInfo(null, null, null, 0); + if (tagInfo instanceof Sequenceable) { + data.readSequenceable((Sequenceable) tagInfo); + } + onTagDiscovered(tagInfo); + + // writeNoException + reply.writeInt(0); + break; + } + default: { + + return super.onRemoteRequest(code, data, reply, option); + } + } + return true; + } + + @Override + public IRemoteObject asObject() { + return this; + } + + @Override + public void onTagDiscovered(TagInfo tagInfo) { + if (callback != null) { + callback.onTagDiscovered(tagInfo); + } + } + } + + private UsbDevicePipeController(Context context) { + mUsbDevicePipeControllerProxy = UsbDevicePipeControllerProxy.getInstance(); + mContext = context; + } + + /** + * Obtains a singleton instance of the {@code NfcController} class. + * + *

If no singleton instance is available, a singleton instance will be created. + * + * @param context Indicates the application context. + * @return Returns a singleton instance. + * @throws IllegalArgumentException if the caller is missing required context. + * @since 1 + */ + public static synchronized UsbDevicePipeController getInstance(Context context) throws IllegalArgumentException { + if (sUsbDevicePipeController == null) { + if (context == null) { + throw new IllegalArgumentException("context is null"); + } + sUsbDevicePipeController = new UsbDevicePipeController(context.getApplicationContext()); + } + return sUsbDevicePipeController; + } + + /** + * get the context of application + * + * @return the context of application + * @hide + */ + public Context getContext() { + return mContext; + } +} \ No newline at end of file diff --git a/interfaces/kits/java/ohos/usb/USBEndpoint.java b/interfaces/kits/java/ohos/usb/USBEndpoint.java new file mode 100644 index 00000000..838d52cc --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBEndpoint.java @@ -0,0 +1,151 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import ohos.utils.system.SystemCap; +import ohos.utils.system.SystemCapability; + +/** + * Get endpoint information about a USB device. + * + *

When using this class, a USB device is attached to a USB host. According to the USB protocol specifications, + * a device contains one or more configurations, a configuration contains one or more interfaces, and an interface + * contains one or more endpoints. + * + * @Syscap {@link SystemCapability.Usb#USB_MANAGER} + * @since 5 + */ +@SystemCap(SystemCapability.Usb.USB_MANAGER) +public class USBEndpoint { + private int address; + + private int attributes; + + private int direction; + + private int number; + + private int interval; + + private int maxPacketSize; + + private int type; + + /** + * Creates a {@code USBEndpoint} instance. + * + * @param address Indicates the USB endpoint address, which consists of the endpoint ID and data transfer direction. + * @param attributes Indicates USB endpoint attributes, such as the data transfer type. + * @param direction Indicates the USB endpoint direction. Value 0 indicates OUT, and value 1 indicates IN. + * @param number Indicates the USB endpoint number, which uniquely identifies a USB endpoint on a USB device. + * @param interval Indicates the polling interval of the USB endpoint during data transfer. + * @param maxPacketSize Indicates the maximum packet size of the USB endpoint, that is, the maximum size of packets + * that can be received or transmitted by the endpoint after it is activated. + * @param type Indicates the USB endpoint type. + * + * @since 5 + */ + public USBEndpoint(int address, int attributes, int direction, int number, int interval, int maxPacketSize, + int type) { + this.address = address; + this.attributes = attributes; + this.direction = direction; + this.number = number; + this.interval = interval; + this.maxPacketSize = maxPacketSize; + this.type = type; + } + + /** + * Get the endpoint address. + * + * @return Returns the endpoint address. + * @since 5 + */ + public int getAddress() { + return address; + } + + /** + * Get the endpoint attributes. + * + * @return Returns the endpoint attributes. + * @since 5 + */ + public int getAttributes() { + return attributes; + } + + /** + * Get the endpoint direction. + * + * @return Returns the endpoint direction. + * @since 5 + */ + public int getDirection() { + return direction; + } + + /** + * Get the endpoint number. + * + * @return Returns the endpoint number. + * @since 5 + */ + public int getEndpointNumber() { + return number; + } + + /** + * Get the endpoint interval. + * + * @return Returns the endpoint interval, in ms. + * @since 5 + */ + public int getInterval() { + return interval; + } + + /** + * Get the maximum packet size of this endpoint. + * + * @return Returns the maximum packet size. + * @since 5 + */ + public int getMaxPacketSize() { + return maxPacketSize; + } + + /** + * Get the endpoint type. + * + * @return Returns the endpoint type. + * @since 5 + */ + public int getType() { + return type; + } + + @Override + public String toString() { + return new StringBuilder( + "USBEndpoint[address=" + address + ", attributes=" + attributes + ", direction=" + direction + ", number=" + + number + ", interval=" + interval + ", maxPacketSize=" + maxPacketSize + ", type=" + type + + "]").toString(); + } +} + + diff --git a/interfaces/kits/java/ohos/usb/USBInterface.java b/interfaces/kits/java/ohos/usb/USBInterface.java new file mode 100644 index 00000000..8034935f --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBInterface.java @@ -0,0 +1,172 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import ohos.utils.system.SystemCap; +import ohos.utils.system.SystemCapability; + +/** + * Get interface and endpoint information about a USB device. + * + *

+ * When using this class, a USB device is attached to a USB host. According to + * the USB protocol specifications, a device contains one or more + * configurations, a configuration contains one or more interfaces, and an + * interface contains one or more endpoints. + * + * @Syscap {@link SystemCapability.Usb#USB_MANAGER} + * @since 5 + */ +@SystemCap(SystemCapability.Usb.USB_MANAGER) +public class USBInterface { + private final int id; + + private final int alternateSetting; + + private final String name; + + private final int classType; + + private final int subclass; + + private final int protocol; + + private USBEndpoint[] endpoints; + + /** + * Creates a {@code USBInterface} object. + * + * @param id Indicates the ID of an interface. + * @param alternateSetting Indicates the alternative configuration of the interface. + * @param name Indicates the interface name. + * @param class Indicates the interface class. + * @param subClass Indicates the interface subclass. + * @param protocol Indicates the protocol supported by the interface. + * + * @since 5 + */ + public USBInterface(int id, int alternateSetting, String name, int Class, int subClass, int protocol) { + this.id = id; + this.alternateSetting = alternateSetting; + this.name = name; + this.classType = Class; + this.subclass = subClass; + this.protocol = protocol; + } + + /** + * Get the {@code AlternateSetting} field of a USB interface. + * + * @return Returns the {@code AlternateSetting} field. + * @since 5 + */ + public int getAlternateSetting() { + return alternateSetting; + } + + /** + * Get the {@code localId} field of a USB interface. + * + * @return Returns the {@code InterfaceId} field. + * @since 5 + */ + public int getId() { + return id; + } + + /** + * Get the {@code InterfaceClass} field of a USB interface. + * + * @return Returns the {@code InterfaceClass} field. + * @since 5 + */ + public int getInterfaceClass() { + return classType; + } + + /** + * Get the {@code InterfaceProtocol} field of a USB interface. + * + * @return Returns the {@code InterfaceProtocol} field. + * @since 5 + */ + public int getInterfaceProtocol() { + return protocol; + } + + /** + * Get the {@code InterfaceSubclass} field of a USB interface. + * + * @return Returns the {@code InterfaceSubclass} field. + * @since 5 + */ + public int getInterfaceSubclass() { + return subclass; + } + + /** + * Get the {@code localName} field of a USB interface. + * + * @return Returns the {@code InterfaceName} field. + * @since 5 + */ + public String getName() { + return name; + } + + /** + * Get a {@code USBEndpoint} instance of this interface by index. + * + * @param index Indicates the endpoint index. + * @return Returns the {@code USBEndpoint} instance. + */ + public USBEndpoint getEndpoint(int index) { + if (endpoints == null || index < 0 || index >= endpoints.length) { + return null; + } + return endpoints[index]; + } + + /** + * Get the number of endpoints. + * + * @return Returns the number of endpoints. + * @since 5 + */ + public int getEndpointCount() { + if (endpoints == null) { + return 0; + } + return endpoints.length; + } + + /** + * Sets the endpoints list for a local {@code USBEndpoint} object. + * + * @param endpoints Indicates the endpoints of the interface. + * @since 5 + */ + public void setEndpoints(USBEndpoint[] endpoints) { + this.endpoints = endpoints; + } + + @Override + public String toString() { + return new StringBuilder( + "USBInterface[id=" + id + ", alternateSetting=" + alternateSetting + ", name=" + name + ", class=" + + classType + ", subclass=" + subclass + ", protocol=" + protocol + "]").toString(); + } +} diff --git a/interfaces/kits/java/ohos/usb/USBLoggerFactory.java b/interfaces/kits/java/ohos/usb/USBLoggerFactory.java new file mode 100644 index 00000000..c9413b64 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBLoggerFactory.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; + +/** + * An USB logger factory class + * + * @since 5 + * {@hide} + */ + +public class USBLoggerFactory { + private static final int DOMAIN = 0xD002A00; + + private static final String DEFAULT_TAG = "UsbService"; + + private USBLoggerFactory() { + } + + public static HiLogLabel getHilogLabel(String tag) { + if (tag == null || tag.isEmpty()) { + return USBLoggerFactory.getHilogLabel(); + } + return new HiLogLabel(HiLog.LOG_CORE, USBLoggerFactory.DOMAIN, tag); + } + + public static HiLogLabel getHilogLabel() { + return new HiLogLabel(HiLog.LOG_CORE, USBLoggerFactory.DOMAIN, DEFAULT_TAG); + } +} diff --git a/interfaces/kits/java/ohos/usb/USBPort.java b/interfaces/kits/java/ohos/usb/USBPort.java new file mode 100644 index 00000000..2a99499d --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBPort.java @@ -0,0 +1,4 @@ +package ohos.usb; + +public class USBPort { +} \ No newline at end of file diff --git a/interfaces/kits/java/ohos/usb/USBRequest.java b/interfaces/kits/java/ohos/usb/USBRequest.java new file mode 100644 index 00000000..926f8ea5 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/USBRequest.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2021 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. + */ + +package ohos.usb; + +import java.nio.ByteBuffer; + +import ohos.utils.system.SystemCap; +import ohos.utils.system.SystemCapability; + +/** + * Implements bulk transfer of data and control messages between USB devices. + * + * @Syscap {@link SystemCapability.Usb#USB_MANAGER} + * @since 5 + */ +@SystemCap(SystemCapability.Usb.USB_MANAGER) +public class USBRequest { + private Object clientData; + + + public USBRequest() { + + } + + /** + * Aborts a pending queue operation. + */ + public void abort() { + native_abort(); + } + + /** + * Frees all resources related to this request. + */ + public void free() { + native_free(); + } + + /** + * Initializes a specified endpoint for data transfer. + * + * @param connection Indicates a {@code USBDevicePipe} instance. + * @param ep Indicates an instance of the {@code USBEndpoint} class. + * @return Returns {@code true} if the initialization is successful; returns {@code false} otherwise. + * @since 5 + */ + public boolean initialize(USBDevicePipe connection, USBEndpoint ep) { + return native_initialize(connection, ep); + } + + /** + * Transfers data on the requested endpoint, and queues the request. + * + * @param buffer Indicates the buffer from which data is to be transferred. + * @return Returns {@code true} if the data transfer is successful; returns {@code false} otherwise. + * @since 5 + */ + public boolean queue(ByteBuffer buffer) { + return native_queue(buffer); + } + + /** + * Transfers data on the requested endpoint, and queues the request. + * + * @param buffer Indicates the buffer from which data is to be transferred. + * @param length Indicates the length of buffer. + * @return Returns {@code true} if the data transfer is successful; returns {@code false} otherwise. + * @since 5 + */ + public boolean queue(ByteBuffer buffer, int length) { + return native_queue(buffer, length); + } + + /** + * Dequeues a request to send or receive data on its endpoint. + * + * @param useBufferOverflowInsteadOfIllegalArg Specifies whether to replace the illegal parameter + * with a buffer overflow. + * @since 5 + */ + public void dequeue(boolean useBufferOverflowInsteadOfIllegalArg) { + native_dequeue(useBufferOverflowInsteadOfIllegalArg); + } + + /** + * Sets client data of the {@code USBRequst} instance. + * + * @param data The client data to be set. + * @since 5 + */ + public void setClientData(Object data) { + native_set_client_data(data); + } + + /** + * Gets client data of the {@code USBRequst} instance. + * + * @since 5 + * @return Returns the client data. + */ + public Object getClientData() { + return clientData; + } + + private native void native_abort(); + + private native void native_free(); + + private native boolean native_initialize(USBDevicePipe connection, USBEndpoint ep); + + private native boolean native_queue(ByteBuffer buffer); + + private native boolean native_queue(ByteBuffer buffer, int length); + + private native void native_set_client_data(Object data); + + private native void native_dequeue(boolean useBufferOverflowInsteadOfIllegalArg); +} diff --git a/interfaces/kits/java/ohos/usb/UsbCoreController.java b/interfaces/kits/java/ohos/usb/UsbCoreController.java new file mode 100644 index 00000000..e2b32a88 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/UsbCoreController.java @@ -0,0 +1,142 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.ability.Lifecycle; +import ohos.app.Context; +import ohos.event.intentagent.IntentAgent; +import ohos.utils.Parcel; +import ohos.utils.Sequenceable; +import ohos.utils.system.SystemCap; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.rpc.MessageParcel; +import ohos.rpc.MessageOption; +import ohos.rpc.RemoteException; +import ohos.rpc.RemoteObject; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.IRemoteObject; +import ohos.interwork.utils.PacMapEx; +import ohos.nfc.tag.TagInfo; + +import java.util.List; + +@SystemCap("SystemCapability.Communication.USB") +public class UsbCoreController { + + private static UsbCoreController sUsbCoreController; + private UsbCoreControllerProxy mUsbCoreControllerProxy; + private Context mContext; + + /** + * A callback to be invoked when the system finds a tag while the foreground page is operating in reader mode. + * + */ + public interface ReaderModeCallback extends IRemoteBroker { + /** + * Indicates the transaction code of the callback specific to inter-process communication (IPC). + */ + int TRANSACTION_ON_CALLBACK = 1; + + /** + * Called when a tag is discovered. + * + * @param tagInfo Indicates information of the discovered tag. + * @since 4 + */ + void onTagDiscovered(TagInfo tagInfo); + } + + /** + * Provides ReaderModeCallback for communicate with remote. + * + * @since 4 + */ + public class ReaderModeCallbackStub extends RemoteObject implements ReaderModeCallback { + private ReaderModeCallback callback; + + public ReaderModeCallbackStub(ReaderModeCallback callback) { + super(); + this.callback = callback; + } + + private void readInterfaceToken(Parcel data) { + data.readInt(); + data.readInt(); + data.readString(); + } + + @Override + public boolean onRemoteRequest( + int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException { + HiLog.debug(LABEL, "call ReaderModeCallbackStub's onTransact"); + + switch (code) { + case TRANSACTION_ON_CALLBACK: { + readInterfaceToken(data); + + TagInfo tagInfo = new TagInfo(null, null, null, 0); + if (tagInfo instanceof Sequenceable) { + data.readSequenceable((Sequenceable) tagInfo); + } + onTagDiscovered(tagInfo); + + // writeNoException + reply.writeInt(0); + break; + } + default: { + + return super.onRemoteRequest(code, data, reply, option); + } + } + return true; + } + + @Override + public IRemoteObject asObject() { + return this; + } + + @Override + public void onTagDiscovered(TagInfo tagInfo) { + if (callback != null) { + callback.onTagDiscovered(tagInfo); + } + } + } + + private UsbCoreController(Context context) { + mUsbCoreControllerProxy = UsbCoreControllerProxy.getInstance(); + mContext = context; + } + + /** + * Obtains a singleton instance of the {@code NfcController} class. + * + *

If no singleton instance is available, a singleton instance will be created. + * + * @param context Indicates the application context. + * @return Returns a singleton instance. + * @throws IllegalArgumentException if the caller is missing required context. + * @since 1 + */ + public static synchronized UsbCoreController getInstance(Context context) throws IllegalArgumentException { + if (sUsbCoreController == null) { + if (context == null) { + throw new IllegalArgumentException("context is null"); + } + sUsbCoreController = new UsbCoreController(context.getApplicationContext()); + } + return sUsbCoreController; + } + + /** + * get the context of application + * + * @return the context of application + * @hide + */ + public Context getContext() { + return mContext; + } +} diff --git a/interfaces/kits/java/ohos/usb/UsbCoreControllerProxy.java b/interfaces/kits/java/ohos/usb/UsbCoreControllerProxy.java new file mode 100644 index 00000000..d356d016 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/UsbCoreControllerProxy.java @@ -0,0 +1,414 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.event.intentagent.IntentAgent; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.interwork.utils.PacMapEx; +import ohos.usb.UsbCoreController.ReaderModeCallback; +import ohos.usb.UsbCoreController.ReaderModeCallbackStub; +import ohos.rpc.IRemoteObject; +import ohos.rpc.IRemoteObject.DeathRecipient; +import ohos.rpc.MessageParcel; +import ohos.rpc.MessageOption; +import ohos.rpc.RemoteException; +import ohos.rpc.RemoteObject; +import ohos.sysability.samgr.SysAbilityManager; +import ohos.utils.Parcel; +import ohos.utils.Sequenceable; +import ohos.utils.system.safwk.java.SystemAbilityDefinition; + +import java.util.List; + +public class UsbCoreControllerProxy implements IUsbCoreController { + + private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_CORE,UsbKitsUtils.USB_DOMAIN_ID, "UsbCoreControllerProxy"); + private static final String USB_CORE_CONTROLLER = "ohos.usb.IUSBCoreController"; + private static UsbCoreControllerProxy sUsbCoreControllerProxy; + private final Object mRemoteLock = new Object(); + private IRemoteObject mRemoteObject; + + private UsbCoreControllerProxy() { + + } + + /** + * Gets a {@cod UsbControllerProxy} instance. + * + * @return Returns the instance of UsbControllerProxy. + */ + public static synchronized UsbCoreControllerProxy getInstance() { + if (sUsbCoreControllerProxy == null) { + sUsbCoreControllerProxy = new UsbCoreControllerProxy(); + } + return sUsbCoreControllerProxy; + } + + @Override + public IRemoteObject asObject() { + if (mRemoteObject != null) { + return mRemoteObject; + } + synchronized (mRemoteLock) { + mRemoteObject = SysAbilityManager.getSysAbility(SystemAbilityDefinition.NFC_MANAGER_SYS_ABILITY_ID); + if (mRemoteObject != null) { + mRemoteObject.addDeathRecipient(new UsbCoreControllerDeathRecipient(), 0); + HiLog.info(LABEL, "Get UsbManagerService completed."); + } else { + HiLog.error(LABEL, "getSysAbility(UsbManagerService) failed."); + } + return mRemoteObject; + } + } + + @Override + public HashMap getDevices() throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(1, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "getDevices call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + public USBDevicePipe connectDevice(USBDevice device) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + boolean hasRight(USBDevice device) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void requestRight(USBDevice device, String intentName) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + String usbFunctionsToString(long functions) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + long usbFunctionsFromString(String functions) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + String getCurrentFunctions() throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void setCurrentFunctions(long functions) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void setCurrentFunction(String functions, boolean usbDataUnlocked) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void setHdcEnabled(boolean enable) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void addFunction(String functions, String function) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void removeFunction(String functions, String function) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void setPortRoles(int powerRole,int dataRole) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + UsbPortStatus getPortStatus(USBPort port) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(2, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "connectDevice call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + // Implementation of IPC Death Recipient. + private class UsbCoreControllerDeathRecipient implements DeathRecipient { + @Override + public void onRemoteDied() { + HiLog.warn(LABEL, "UsbCoreControllerDeathRecipient::onRemoteDied."); + setRemoteObject(null); + } + } + + private void setRemoteObject(IRemoteObject remoteObject) { + synchronized (mRemoteLock) { + mRemoteObject = remoteObject; + } + } +} diff --git a/interfaces/kits/java/ohos/usb/UsbDevicePipeControllerProxy.java b/interfaces/kits/java/ohos/usb/UsbDevicePipeControllerProxy.java new file mode 100644 index 00000000..9432ac91 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/UsbDevicePipeControllerProxy.java @@ -0,0 +1,173 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.event.intentagent.IntentAgent; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.interwork.utils.PacMapEx; +import ohos.usb.UsbDevicePipeController.ReaderModeCallback; +import ohos.usb.UsbDevicePipeController.ReaderModeCallbackStub; +import ohos.rpc.IRemoteObject; +import ohos.rpc.IRemoteObject.DeathRecipient; +import ohos.rpc.MessageParcel; +import ohos.rpc.MessageOption; +import ohos.rpc.RemoteException; +import ohos.rpc.RemoteObject; +import ohos.sysability.samgr.SysAbilityManager; +import ohos.utils.Parcel; +import ohos.utils.Sequenceable; +import ohos.utils.system.safwk.java.SystemAbilityDefinition; + +import java.util.List; + +public class UsbDevicePipeControllerProxy implements IUsbDevicePipeController { + + private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_CORE,UsbKitsUtils.USB_DOMAIN_ID, "UsbDevicePipeControllerProxy"); + private static final String USB_DevicePipe_CONTROLLER = "ohos.usb.IUSBDevicePipeController"; + private static UsbDevicePipeControllerProxy sUsbDevicePipeControllerProxy; + private final Object mRemoteLock = new Object(); + private IRemoteObject mRemoteObject; + + private UsbDevicePipeControllerProxy() { + + } + + /** + * Gets a {@cod UsbControllerProxy} instance. + * + * @return Returns the instance of UsbControllerProxy. + */ + public static synchronized UsbDevicePipeControllerProxy getInstance() { + if (sUsbDevicePipeControllerProxy == null) { + sUsbDevicePipeControllerProxy = new UsbDevicePipeControllerProxy(); + } + return sUsbDevicePipeControllerProxy; + } + + @Override + public IRemoteObject asObject() { + if (mRemoteObject != null) { + return mRemoteObject; + } + synchronized (mRemoteLock) { + mRemoteObject = SysAbilityManager.getSysAbility(SystemAbilityDefinition.NFC_MANAGER_SYS_ABILITY_ID); + if (mRemoteObject != null) { + mRemoteObject.addDeathRecipient(new UsbDevicePipeControllerDeathRecipient(), 0); + HiLog.info(LABEL, "Get UsbManagerService completed."); + } else { + HiLog.error(LABEL, "getSysAbility(UsbManagerService) failed."); + } + return mRemoteObject; + } + } + + @Override + int bulkTransfer(USBEndpoint epObj, byte[] buffer, int length, int timeout) throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_DevicePipe_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(1, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "getDevices call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + int bulkTransfer(USBEndpoint epObj, byte[] buffer, int offset, int length, int timeout) throws RemoteException { + + } + + @Override + boolean claimInterface(USBInterface interfaceObj, boolean force) throws RemoteException { + + } + + @Override + void close() throws RemoteException { + + } + + @Override + int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int offset, int length, int timeout) throws RemoteException { + + } + + @Override + int controlTransfer(int requestType, int request, int value, int index, byte[] buffer, int length, int timeout) throws RemoteException { + + } + + @Override + int getFileDescriptor() throws RemoteException { + + } + + @Override + byte[] getRawDescriptors() throws RemoteException { + + } + + @Override + String getSerial() throws RemoteException { + + } + + @Override + boolean releaseInterface(USBInterface interfaceObj) throws RemoteException { + + } + + @Override + USBRequest requestWait(long timeout) throws TimeoutException, RemoteException{ + + } + + @Override + USBRequest requestWait() throws RemoteException { + + } + + @Override + boolean setConfiguration(USBConfig config) throws RemoteException { + + } + + @Override + boolean setInterface(USBInterface interfaceObj) throws RemoteException { + + } + + @Override + boolean openByInt(String name, int fd) throws RemoteException { + + } + + // Implementation of IPC Death Recipient. + private class UsbDevicePipeControllerDeathRecipient implements DeathRecipient { + @Override + public void onRemoteDied() { + HiLog.warn(LABEL, "UsbDevicePipeControllerDeathRecipient::onRemoteDied."); + setRemoteObject(null); + } + } + + private void setRemoteObject(IRemoteObject remoteObject) { + synchronized (mRemoteLock) { + mRemoteObject = remoteObject; + } + } +} \ No newline at end of file diff --git a/interfaces/kits/java/ohos/usb/UsbKitsUtils.java b/interfaces/kits/java/ohos/usb/UsbKitsUtils.java new file mode 100644 index 00000000..b85024cd --- /dev/null +++ b/interfaces/kits/java/ohos/usb/UsbKitsUtils.java @@ -0,0 +1,7 @@ +package ohos.usb; + +public class UsbKitsUtils { + + /** USB Domain ID. */ + public static final int USB_DOMAIN_ID = 0xD002900; +} diff --git a/interfaces/kits/java/ohos/usb/UsbPortStatus.java b/interfaces/kits/java/ohos/usb/UsbPortStatus.java new file mode 100644 index 00000000..c820c43d --- /dev/null +++ b/interfaces/kits/java/ohos/usb/UsbPortStatus.java @@ -0,0 +1,4 @@ +package ohos.usb; + +public class UsbPortStatus { +} \ No newline at end of file diff --git a/interfaces/kits/java/ohos/usb/UsbRequestController.java b/interfaces/kits/java/ohos/usb/UsbRequestController.java new file mode 100644 index 00000000..68d63bb9 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/UsbRequestController.java @@ -0,0 +1,142 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.aafwk.ability.Lifecycle; +import ohos.app.Context; +import ohos.event.intentagent.IntentAgent; +import ohos.utils.Parcel; +import ohos.utils.Sequenceable; +import ohos.utils.system.SystemCap; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.rpc.MessageParcel; +import ohos.rpc.MessageOption; +import ohos.rpc.RemoteException; +import ohos.rpc.RemoteObject; +import ohos.rpc.IRemoteBroker; +import ohos.rpc.IRemoteObject; +import ohos.interwork.utils.PacMapEx; +import ohos.nfc.tag.TagInfo; + +import java.util.List; + +@SystemCap("SystemCapability.Communication.USB") +public class UsbRequestController { + + private static UsbRequestController sUsbRequestController; + private UsbRequestControllerProxy mUsbRequestControllerProxy; + private Context mContext; + + /** + * A callback to be invoked when the system finds a tag while the foreground page is operating in reader mode. + * + */ + public interface ReaderModeCallback extends IRemoteBroker { + /** + * Indicates the transaction code of the callback specific to inter-process communication (IPC). + */ + int TRANSACTION_ON_CALLBACK = 1; + + /** + * Called when a tag is discovered. + * + * @param tagInfo Indicates information of the discovered tag. + * @since 4 + */ + void onTagDiscovered(TagInfo tagInfo); + } + + /** + * Provides ReaderModeCallback for communicate with remote. + * + * @since 4 + */ + public class ReaderModeCallbackStub extends RemoteObject implements ReaderModeCallback { + private ReaderModeCallback callback; + + public ReaderModeCallbackStub(ReaderModeCallback callback) { + super(); + this.callback = callback; + } + + private void readInterfaceToken(Parcel data) { + data.readInt(); + data.readInt(); + data.readString(); + } + + @Override + public boolean onRemoteRequest( + int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException { + HiLog.debug(LABEL, "call ReaderModeCallbackStub's onTransact"); + + switch (code) { + case TRANSACTION_ON_CALLBACK: { + readInterfaceToken(data); + + TagInfo tagInfo = new TagInfo(null, null, null, 0); + if (tagInfo instanceof Sequenceable) { + data.readSequenceable((Sequenceable) tagInfo); + } + onTagDiscovered(tagInfo); + + // writeNoException + reply.writeInt(0); + break; + } + default: { + + return super.onRemoteRequest(code, data, reply, option); + } + } + return true; + } + + @Override + public IRemoteObject asObject() { + return this; + } + + @Override + public void onTagDiscovered(TagInfo tagInfo) { + if (callback != null) { + callback.onTagDiscovered(tagInfo); + } + } + } + + private UsbRequestController(Context context) { + mUsbRequestControllerProxy = UsbRequestControllerProxy.getInstance(); + mContext = context; + } + + /** + * Obtains a singleton instance of the {@code NfcController} class. + * + *

If no singleton instance is available, a singleton instance will be created. + * + * @param context Indicates the application context. + * @return Returns a singleton instance. + * @throws IllegalArgumentException if the caller is missing required context. + * @since 1 + */ + public static synchronized UsbRequestController getInstance(Context context) throws IllegalArgumentException { + if (sUsbRequestController == null) { + if (context == null) { + throw new IllegalArgumentException("context is null"); + } + sUsbRequestController = new UsbRequestController(context.getApplicationContext()); + } + return sUsbRequestController; + } + + /** + * get the context of application + * + * @return the context of application + * @hide + */ + public Context getContext() { + return mContext; + } +} diff --git a/interfaces/kits/java/ohos/usb/UsbRequestControllerProxy.java b/interfaces/kits/java/ohos/usb/UsbRequestControllerProxy.java new file mode 100644 index 00000000..27a88791 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/UsbRequestControllerProxy.java @@ -0,0 +1,134 @@ +package ohos.usb; + +import ohos.aafwk.ability.Ability; +import ohos.event.intentagent.IntentAgent; +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; +import ohos.interwork.utils.PacMapEx; +import ohos.usb.UsbRequestController.ReaderModeCallback; +import ohos.usb.UsbRequestController.ReaderModeCallbackStub; +import ohos.rpc.IRemoteObject; +import ohos.rpc.IRemoteObject.DeathRecipient; +import ohos.rpc.MessageParcel; +import ohos.rpc.MessageOption; +import ohos.rpc.RemoteException; +import ohos.rpc.RemoteObject; +import ohos.sysability.samgr.SysAbilityManager; +import ohos.utils.Parcel; +import ohos.utils.Sequenceable; +import ohos.utils.system.safwk.java.SystemAbilityDefinition; + +import java.util.List; + +public class UsbRequestControllerProxy implements IUsbRequestController { + + private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_CORE,UsbKitsUtils.USB_DOMAIN_ID, "UsbRequestControllerProxy"); + private static final String USB_Request_CONTROLLER = "ohos.usb.IUSBRequestController"; + private static UsbRequestControllerProxy sUsbRequestControllerProxy; + private final Object mRemoteLock = new Object(); + private IRemoteObject mRemoteObject; + + private UsbRequestControllerProxy() { + + } + + /** + * Gets a {@cod UsbControllerProxy} instance. + * + * @return Returns the instance of UsbControllerProxy. + */ + public static synchronized UsbRequestControllerProxy getInstance() { + if (sUsbRequestControllerProxy == null) { + sUsbRequestControllerProxy = new UsbRequestControllerProxy(); + } + return sUsbRequestControllerProxy; + } + + @Override + public IRemoteObject asObject() { + if (mRemoteObject != null) { + return mRemoteObject; + } + synchronized (mRemoteLock) { + mRemoteObject = SysAbilityManager.getSysAbility(SystemAbilityDefinition.NFC_MANAGER_SYS_ABILITY_ID); + if (mRemoteObject != null) { + mRemoteObject.addDeathRecipient(new UsbRequestControllerDeathRecipient(), 0); + HiLog.info(LABEL, "Get UsbManagerService completed."); + } else { + HiLog.error(LABEL, "getSysAbility(UsbManagerService) failed."); + } + return mRemoteObject; + } + } + + @Override + void abort() throws RemoteException { + IRemoteObject service = asObject(); + if (service == null) { + throw new RemoteException("service is null"); + } + MessageParcel data = MessageParcel.obtain(); + MessageParcel reply = MessageParcel.obtain(); + MessageOption option = new MessageOption(); + try { + data.writeString(USB_CORE_CONTROLLER); + data.writeInt(isEnabled ? TRUE : FALSE); + // TODO 第一个参数是硬编码,参数定义在UsbKitsUtil,待补充 + service.sendRequest(1, data, reply, option); + // TODO 返回值待处理,从reply中获取 + return null; + } catch (RemoteException remoteException) { + HiLog.error(LABEL, "getDevices call failed."); + throw remoteException; + } finally { + reply.reclaim(); + data.reclaim(); + } + } + + @Override + void free() throws RemoteException { + + } + + @Override + boolean initialize(USBDevicePipe connection, USBEndpoint ep) throws RemoteException { + + } + + @Override + boolean queue(ByteBuffer buffer) throws RemoteException { + + } + + @Override + boolean queue(ByteBuffer buffer, int length) throws RemoteException { + + } + + @Override + void dequeue(boolean useBufferOverflowInsteadOfIllegalArg) throws RemoteException { + + } + + @Override + void setClientData(Object data) throws RemoteException { + + } + + + // Implementation of IPC Death Recipient. + private class UsbRequestControllerDeathRecipient implements DeathRecipient { + @Override + public void onRemoteDied() { + HiLog.warn(LABEL, "UsbRequestControllerDeathRecipient::onRemoteDied."); + setRemoteObject(null); + } + } + + private void setRemoteObject(IRemoteObject remoteObject) { + synchronized (mRemoteLock) { + mRemoteObject = remoteObject; + } + } +} diff --git a/interfaces/kits/java/ohos/usb/package-info.java b/interfaces/kits/java/ohos/usb/package-info.java new file mode 100644 index 00000000..f7a002e0 --- /dev/null +++ b/interfaces/kits/java/ohos/usb/package-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2021 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. + */ + +/** + * Provides basic USB communication capabilities for other peripherals and devices running OHOS. + * + *

The basic USB communication capabilities include control transfer and bulk transfer. + * + * @since 5 + */ + +package ohos.usb; \ No newline at end of file diff --git a/interfaces/kits/js/@ohos.usb.d.ts b/interfaces/kits/js/@ohos.usb.d.ts new file mode 100644 index 00000000..e3607d1d --- /dev/null +++ b/interfaces/kits/js/@ohos.usb.d.ts @@ -0,0 +1,879 @@ +/* + * Copyright (c) 2021 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 { + /** + * just for test + */ + function getVersion(): string; + /* usb core functions begin */ + /** + * 获取USB设备列表 + * + * @return 设备信息{@link USBDevice}列表. + * @since 8 + */ + function getDevices(): Array>; + + /** + * 根据{@link getDevices()}返回的设备信息打开USB设备 + * + * @param device 设备信息,{@link getDevices()}返回的列表中的一个设备信息 + * @return 指定的传输通道 {@link USBDevicePipe} 对象. + * @since 8 + */ + function connectDevice(device: USBDevice): Readonly; + + /** + * 判断是否有权访问设备 + * + * @param deviceName 设备名称,{@link USBDevice.name} + * @return true 有权限, false 没有权限 + * @since 8 + */ + function hasRight(deviceName: string): boolean; + + /** + * 请求给定软件包的临时权限以访问设备 + * + * @param deviceName 设备名称,{@link USBDevice.name} + * @return true 请求权限成功, false 请求权限失败 + * @since 8 + */ + function requestRight(deviceName: string): Promise; + + /** + * 将给定的功能列表描述字符串转换为功能列表的数字组合掩码 + * + * @param funcs 支持的功能列表描述符 + * @return 功能列表的数字组合掩码 + * @systemapi + * @since 8 + */ + function usbFunctionsFromString(funcs: string): number; + + /** + * 将给定的功能列表的数字组合掩码转换为功能列表描述字符串 + * + * @param funcs 支持的功能列表的数字组合掩码 + * @return 支持的功能列表描述字符串 + * @systemapi + * @since 8 + */ + function usbFunctionsToString(funcs: FunctionType): string; + + /** + * 在设备模式下设置当前的USB功能列表 + * + * @param funcs 设置支持的功能列表的数字组合掩码{@link FunctionType} + * @return true 设置成功, false 设置失败 + * @systemapi + * @since 8 + */ + function setCurrentFunctions(funcs: FunctionType): Promise; + /** + * 获取设备模式下的当前USB功能列表的数字组合掩码 + * + * @return 支持的功能列表的数字组合掩码{@link FunctionType} + * @systemapi + * @since 8 + */ + function getCurrentFunctions(): FunctionType; + + /* usb port functions begin */ + /** + * 获取物理USB端口描述信息{@link USBPort}列表 + * + * @return {@link USBPort}列表 + * @systemapi + * @since 8 + */ + function getPorts(): Array; + + /** + * 获取指定的端口{@link USBPort}支持的模式列表的组合掩码 + * + * @return 支持的模式列表的组合掩码{@link PortModeType} + * @systemapi + * @since 8 + */ + function getSupportedModes(portId: number): PortModeType; + + /** + * 设置指定的端口{@link USBPort}支持的角色模式,包含充电角色、数据传输角色 + * + * @param portId 端口的唯一标识 + * @param powerRole 充电的角色{@link PowerRoleType} + * @param dataRole 数据传输的角色{@link DataRoleType} + * @return 支持的模式 + * @systemapi + * @since 8 + */ + function setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise; + + /* usb pipe functions begin */ + /** + * 获取接口 + * + * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. + * @param iface 用于确定需要获取接口的索引 {@link USBInterface}. + * @param force 是否强制获取 + * @return 成功:0;失败:错误码 + * @since 8 + */ + function claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number; + /** + * 释放接口 + * + * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. + * @param iface 用于确定需要释放接口的索引 {@link USBInterface}. + * @return 成功:0;失败:错误码 + * @since 8 + */ + function releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number; + /** + * 设置设备配置 + * + * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. + * @param config 用于确定需要设置配置的索引 {@link USBConfig}. + * @return 成功:0;失败:错误码 + * @since 8 + */ + function setConfiguration(pipe: USBDevicePipe, config: USBConfig): number; + /** + * 设置设备接口 + * + * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. + * @param iface 用于确定需要设置接口的索引 {@link USBInterface}. + * @return 成功:0;失败:错误码 + * @since 8 + */ + function setInterface(pipe: USBDevicePipe, iface: USBInterface): number; + /** + * 获取原始的USB描述符 + * + * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. + * @return 返回获取的原始数据 + * @since 8 + */ + function getRawDescriptor(pipe: USBDevicePipe): Uint8Array; + /** + * 获取文件描述符 + * + * @param pipe 用于确定设备 {@link USBDevicePipe}. + * @return 返回设备对应的文件描述符 + * @since 8 + */ + function getFileDescriptor(pipe: USBDevicePipe): number; + /** + * 控制传输 + * + * @param pipe 用于确定设备 {@link USBDevicePipe}. + * @param contrlparam 控制传输参数 + * @param timeout 超时时间,可选参数,默认为0不超时 + * @return 传输或接收到的数据块大小,异常返回-1 + * @since 8 + */ + function controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise; + + /** + * 批量传输 + * + * @param pipe 用于确定设备 {@link USBDevicePipe}. + * @param endpoint 用于确定传输的端口 {@link USBEndpoint}. + * @param buffer 用于写入或读取的缓冲区 + * @param timeout 超时时间,可选参数,默认为0不超时 + * @return 传输或接收到的数据块大小,异常返回-1 + * @since 8 + */ + function bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, + timeout?: number): Promise; + + /** + * 关闭USBDevicePipe + * + * @param pipe 用于确定pipe {@link USBDevicePipe}. + * @return 成功:0;失败:错误码 + * @since 8 + */ + function closePipe(pipe: USBDevicePipe): number; + + + /* usb request functions begin */ + + // /** + // * 初始化以指定的端点以进行数据传输 + // * + // * @param pipe USB设备消息控制通道 {@link USBDevicePipe} + // * @param endpoint 端点{@link USBEndpoint} + // * @return {@link USBRequest} 数据传输类 + // * @since 8 + // */ + // function initializeRequest(pipe: USBDevicePipe, endpoint: USBEndpoint): Readonly; + + // /** + // * 关闭数据传输,释放资源 + // * + // * @param request 需要关闭的{@link USBRequest} 数据传输类 + // * @return 成功:0;失败:错误码 + // * @since 8 + // */ + // function finalizeRequest(request: USBRequest): number; + + // /** + // * 取消数据传输 + // * + // * @param request 需要取消的{@link USBRequest} 数据传输类 + // * @return 成功:0;失败:错误码 + // * @since 8 + // */ + // function abortRequest(request: USBRequest): number; + + // /** + // * 接收或发送数据 + // * + // * @param request 指定的{@link USBRequest} 数据传输类 + // * @param buffer 接收或发送的数据缓冲区 + // * @return 传输或接收到的数据块大小,异常返回-1 + // * @since 8 + // */ + // function queueRequest(request: USBRequest, buffer: Uint8Array): Promise; + + /** + * 通过USB发送和接收数据的端口。通过{@link USBInterface}获取。 + * + * @since 8 + */ + interface USBEndpoint { + /** + * 端点地址 + * + * @since 8 + */ + address: number; + + /** + * 端点属性 + * + * @since 8 + */ + attributes: number; + + /** + * 端点间隔 + * + * @since 8 + */ + interval: number; + + /** + * 端点最大数据包大小 + * + * @since 8 + */ + maxPacketSize: number; + + /** + * 端点的方向。 + * + * @since 8 + */ + direction: USBRequestDirection; + + /** + * 端点号 + * + * @since 8 + */ + number: number; + + /** + * 端点类型 + * + * @since 8 + */ + type: number; + + /** + * 端点所属的接口的唯一标识{@link USBInterface.id} + * + * @since 8 + */ + interfaceId: number; + } + + + /** + * 一个{@link USBConfig}中可以含有多个interface,每个interface提供一个功能。 + * + * @since 8 + */ + interface USBInterface { + /** + * 接口的唯一标识 + * + * @since 8 + */ + id: number; + + /** + * 接口的协议 + * + * @since 8 + */ + protocol: number; + + /** + * 设备类型 + * + * @since 8 + */ + clazz: number; + + /** + * 设备子类 + * + * @since 8 + */ + subClass: number; + + /** + *在同一个接口中的多个描述符中进行切换设置 + * + * @since 8 + */ + alternateSetting: number; + + /** + * 接口名称 + * + * @since 8 + */ + name: string; + + /** + * 当前接口所包含的端点{@link USBEndpoint} + * + * @since 8 + */ + endpoints: Array; + } + + /** + * USB配置,一个{@link USBDevice}中可以含有多个配置。 + * + * @since 8 + */ + interface USBConfig { + /** + * 配置的唯一标识 + * + * @since 8 + * + * + */ + id: number; + + /** + * 配置的属性 + * + * @since 8 + */ + attributes: number; + + /** + * 最大功耗,以毫安为单位 + * + * @since 8 + */ + maxPower: number; + + /** + * 配置的名称,可以为空 + * + * @since 8 + */ + name: string; + + /** + * 检查当前配置是否支持远程唤醒 + * + * @since 8 + */ + isRemoteWakeup: boolean; + + /** + * 检查当前配置是否支持独立电源 + * + * @since 8 + */ + isSelfPowered: boolean; + + /** + * 配置支持的接口属性{@link USBConfig} + * + * @since 8 + */ + interfaces: Array; + } + + /** + * USB设备信息。 + * + * @since 8 + */ + interface USBDevice { + /** + * 总线地址 + * + * @since 8 + */ + busNum: number; + /** + * 设备地址 + * + * @since 8 + */ + devAddress: number; + /** + * 序列号 + * + * @since 8 + */ + serial: string; + /** + * 设备名字 + * + * @since 8 + */ + name: string; + /** + * 产商信息 + * + * @since 8 + */ + manufacturerName: string; + /** + * 产品信息 + * + * @since 8 + */ + productName: string; + /** + * 版本 + * + * @since 8 + */ + version: string; + /** + * 厂商ID + * + * @since 8 + */ + vendorId: number; + /** + * 产品ID + * + * @since 8 + */ + productId: number; + /** + * 设备类 + * + * @since 8 + */ + clazz: number; + /** + * 设备子类 + * + * @since 8 + */ + subClass: number; + /** + * 设备协议码 + * + * @since 8 + */ + protocol: number; + /** + * 设备配置描述符信息 {@link USBConfig}. + * + * @since 8 + */ + configs: Array; + } + + /** + * USB设备消息控制通道,用于确定设备 + * + * @since 8 + */ + interface USBDevicePipe { + /** + * 文件描述符 + * + * @since 8 + */ + fd: number; + /** + * 序列号 + * + * @since 8 + */ + serial: string; + /** + * 总线地址 + * + * @since 8 + */ + busNum: number; + /** + * 设备地址 + * + * @since 8 + */ + devAddress: number; + } + + /** + * 表示USB请求数据包的类。 + * + * @since 8 + */ + interface USBRequest { + /** + * USB设备消息控制通道 {@link USBDevicePipe} + * + * @since 8 + */ + pipe: USBDevicePipe; + + /** + * 端点 {@link USBEndpoint} + * + * @since 8 + */ + endpoint: USBEndpoint; + + /** + * 字节的缓冲区,包含要发送的或要填充的缓冲区。最大长度 {@link USBEndpoint.maxPacketSize} + * + * @since 8 + */ + buffer: Uint8Array; + + /** + * 客户端设置的标识数据,用于区分不同的请求 + * + * @since 8 + */ + clientData: Uint8Array; + } + + /** + * 电源角色类型 + * + * @since 8 + */ + enum PowerRoleType { + /** + * 无 + * + * @since 8 + */ + NONE = 0, + /** + * 外部供电 + * + * @since 8 + */ + SOURCE, + /** + * 内部供电 + * + * @since 8 + */ + SINK + } + + /** + * 数据角色类型 + * + * @since 8 + */ + enum DataRoleType { + /** + * 无 + * + * @since 8 + */ + NONE = 0, + /** + * host模式,主设备模式 + * + * @since 8 + */ + HOST, + /** + * device模式,从设备模式 + * + * @since 8 + */ + DEVICE + } + + /** + * port模式类型 + * + * @since 8 + */ + enum PortModeType { + /** + * 无 + * + * @since 8 + */ + NONE = 0, + /** + * host 数据下行,对外提供电源 + * + * @since 8 + */ + DFP, + /** + * device 数据上行,需要外部供电 + * + * @since 8 + */ + UFP + } + + /** + * USB设备端口角色信息 + * + * @since 8 + */ + interface USBPortStatus { + /** + * 当前的USB模式 + * + * @since 8 + */ + currentMode: number; + + /** + * 当前设备充电模式 + * + * @since 8 + */ + currentPowerRole: number; + + /** + * 当前设备数据传输模式 + * + * @since 8 + */ + currentDataRole: number; + } + + /** + * USB设备端口信息 + * + * @since 8 + */ + interface USBPort { + /** + * USB端口信息唯一标识 + * + * @since 8 + */ + id: number; + + /** + * 当前端口支持的模式列表的组合掩码 + * + * @since 8 + */ + supportedModes: number; + + /** + * 当前端口角色信息 {@link USBPortStatus} + * + * @since 8 + */ + status: USBPortStatus; + } + + /** + * 控制传输参数 + * + * @since 8 + */ + interface USBControlParams { + /** + * 请求类型 + * + * @since 8 + */ + request: number; + /** + * 请求目标类型 + * + * @since 8 + */ + target: USBRequestTargetType; + /** + * 控制请求类型 + * + * @since 8 + */ + reqType: USBControlRequestType; + /** + * 请求方向 + * + * @since 8 + */ + directon: USBRequestDirection; + /** + * 请求参数 + * + * @since 8 + */ + value: number; + /** + * 根据请求参数value的不同有所不同 + * + * @since 8 + */ + index: number; + /** + * @用于写入或读取的缓冲区 + * @since 8 + */ + data: Uint8Array; + } + + /** + * 请求目标类型 + * + * @since 8 + */ + enum USBRequestTargetType { + /** + * 设备 + * + * @since 8 + */ + USB_REQUEST_TARGET_DEVICE = 0, + /** + * 接口 + * + * @since 8 + */ + USB_REQUEST_TARGET_INTERFACE, + /** + * 端点 + * + * @since 8 + */ + USB_REQUEST_TARGET_ENDPOINT, + /** + * 其他 + * + * @since 8 + */ + USB_REQUEST_TARGET_OTHER + } + + /** + * 控制请求类型 + * @since 8 + */ + enum USBControlRequestType { + /** + * 标准 + * + * @since 8 + */ + USB_REQUEST_TYPE_STANDARD = 0, + /** + * 类 + * + * @since 8 + */ + USB_REQUEST_TYPE_CLASS, + /** + * 厂商 + * + * @since 8 + */ + USB_REQUEST_TYPE_VENDOR + } + + /** + * 请求方向 + * @since 8 + */ + enum USBRequestDirection { + /** + * 写数据,主设备往从设备 + * + * @since 8 + */ + USB_REQUEST_DIR_TO_DEVICE = 0, + /** + * 读数据,从设备往主设备 + * + * @since 8 + */ + USB_REQUEST_DIR_FROM_DEVICE = 0x80 + } + + + /** + * function 模式 + * + * @since 8 + */ + enum FunctionType { + /** + * 无 + * + * @since 8 + */ + NONE = 0, + /** + * 串口设备 + * + * @since 8 + */ + ACM = 1, + /** + * 网口设备 + * + * @since 8 + */ + ECM = 2, + /** + * HDC设备 + * + * @since 8 + */ + HDC = 4 + } + +} + +export default usb; diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn new file mode 100644 index 00000000..cc25288f --- /dev/null +++ b/interfaces/kits/js/napi/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") + +config("usbnapi_private_config") { + include_dirs = [ + "//foundation/ace/napi/native_engine", + "//foundation/ace/napi/interfaces/kits", + "//utils/system/safwk/native/include", + "//third_party/node/src", + "//base/usbmgr/usb_manager/interfaces/innerkits/native/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", + "//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include", + ] +} + +ohos_shared_library("usb") { + sources = [ "usb_info.cpp" ] + configs = [ + "${utils_path}:utils_config", + ":usbnapi_private_config", + ] + deps = [ + "//base/usb/usb_manager/interfaces/innerkits:usbsrv_client", + "//foundation/ace/napi:ace_napi", + "//utils/native/base:utils", + ] + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + relative_install_dir = "module" + + subsystem_name = "usb" + part_name = "usb_manager_native" +} diff --git a/interfaces/kits/js/napi/usb_async_context.h b/interfaces/kits/js/napi/usb_async_context.h new file mode 100644 index 00000000..ebde5476 --- /dev/null +++ b/interfaces/kits/js/napi/usb_async_context.h @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2021 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. + */ +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "usb_endpoint.h" +#include "usb_pipe.h" +#include "usb_request.h" +#include + +namespace OHOS { +namespace USB { + +struct USBAsyncContext { + napi_env env; + napi_async_work work; + + napi_deferred deferred; + napi_status status; +}; + +struct USBRightAsyncContext : USBAsyncContext { + std::string deviceName; + bool hasRight = false; +}; + +struct USBFunctionAsyncContext : USBAsyncContext { + int32_t functions; +}; + +struct USBPortRoleAsyncContext : USBAsyncContext { + int32_t portId; + int32_t powerRole; + int32_t dataRole; +}; + +struct USBControlTransferAsyncContext : USBAsyncContext { + USBDevicePipe pipe; + int32_t request; + int32_t target; + int32_t reqType; + int32_t directon; + int32_t value; + int32_t index; + uint8_t *buffer; + uint32_t bufferLength; + int32_t timeout = 0; +}; + +struct USBBulkTransferAsyncContext : USBAsyncContext { + USBDevicePipe pipe; + USBEndpoint endpoint; + uint8_t *buffer; + uint32_t bufferLength; + int32_t timeout = 0; +}; + +struct USBQueueAsyncContext : USBAsyncContext { + UsbRequest req; + uint8_t *buffer; + uint32_t bufferLength; +}; +} // namespace USB +} // namespace OHOS diff --git a/interfaces/kits/js/napi/usb_info.cpp b/interfaces/kits/js/napi/usb_info.cpp new file mode 100644 index 00000000..062a6829 --- /dev/null +++ b/interfaces/kits/js/napi/usb_info.cpp @@ -0,0 +1,1728 @@ +/* + * Copyright (c) 2021 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. + */ + +#include +#include +#include +#include +#include +#include "securec.h" +#include "usb_srv_client.h" +#include "hilog_wrapper.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "usb_request.h" +#include "usb_pipe.h" +#include "usb_endpoint.h" +#include "usb_async_context.h" +#include + + +using namespace OHOS; +using namespace OHOS::USB; +const int32_t MSEC_TIME = 1000; + +static void ParseUsbDevicePipe(napi_env env, napi_value &obj, USBDevicePipe &pip); + +static void JsValueToString(const napi_env& env, const napi_value& value, const int bufLen, std::string& target) +{ + char *buf = (char *)malloc(bufLen); + + if (buf == nullptr) { + USB_HILOGE(MODULE_JS_NAPI, "[USB Js] js object to str malloc failed"); + } + (void)memset_s(buf, bufLen, 0, bufLen); + size_t result = 0; + napi_get_value_string_utf8(env, value, buf, bufLen, &result); + + target = buf; + free(buf); + buf = nullptr; +} +static void JsObjectToString(const napi_env& env, const napi_value& object, const char* fieldStr, const int bufLen, + std::string& fieldRef) +{ + if (bufLen <= 0) return; + + bool hasProperty = false; + napi_has_named_property(env, object, fieldStr, &hasProperty); + if (hasProperty) { + napi_value field; + napi_valuetype valueType; + + napi_get_named_property(env, object, fieldStr, &field); + napi_typeof(env, field, &valueType); + NAPI_ASSERT_RETURN_VOID(env, valueType == napi_string, "Wrong argument type. String expected."); + char *buf = (char *)malloc(bufLen); + if (buf == nullptr) { + USB_HILOGE(MODULE_JS_NAPI, "[USB Js] js object to str malloc failed"); + } + (void)memset_s(buf, bufLen, 0, bufLen); + size_t result = 0; + napi_get_value_string_utf8(env, field, buf, bufLen, &result); + fieldRef = buf; + free(buf); + buf = nullptr; + } + else { + USB_HILOGW(MODULE_JS_NAPI, "[USB Js] usb napi js to str no property: %{public}s", fieldStr); + } +} + +static bool JsObjectGetProperty(const napi_env& env, const napi_value& object, const char* fieldStr, napi_value *value) +{ + bool hasProperty = false; + napi_has_named_property(env, object, fieldStr, &hasProperty); + if (hasProperty) { + napi_get_named_property(env, object, fieldStr, value); + } + else { + USB_HILOGW(MODULE_JS_NAPI, "[USB Js] usb napi js to int no property: %{public}s", fieldStr); + } + return hasProperty; +} + +static void JsObjectToInt(const napi_env& env, const napi_value& object, const char* fieldStr, int& fieldRef) +{ + bool hasProperty = false; + napi_has_named_property(env, object, fieldStr, &hasProperty); + if (hasProperty) { + napi_value field; + napi_valuetype valueType; + + napi_get_named_property(env, object, fieldStr, &field); + napi_typeof(env, field, &valueType); + NAPI_ASSERT_RETURN_VOID(env, valueType == napi_number, "Wrong argument type. Number expected."); + napi_get_value_int32(env, field, &fieldRef); + } + else { + USB_HILOGW(MODULE_JS_NAPI, "[USB Js] usb napi js to int no property: %{public}s", fieldStr); + } +} + +// static void JsObjectToBool(const napi_env& env, const napi_value& object, const char* fieldStr, bool& fieldRef) +// { +// bool hasProperty = false; +// napi_has_named_property(env, object, fieldStr, &hasProperty); +// if (hasProperty) { +// napi_value field; +// napi_valuetype valueType; + +// napi_get_named_property(env, object, fieldStr, &field); +// napi_typeof(env, field, &valueType); +// NAPI_ASSERT_RETURN_VOID(env, valueType == napi_boolean, "Wrong argument type. Bool expected."); +// napi_get_value_bool(env, field, &fieldRef); +// } +// else { +// USB_HILOGW(MODULE_JS_NAPI, "[USB Js] usb napi js to bool no property: %{public}s", fieldStr); +// } +// } + +// static napi_value CreateUndefined(napi_env env) +// { +// napi_value result = nullptr; +// napi_get_undefined(env, &result); +// return result; +// } + +static bool JsUint8ArrayParse(const napi_env &env, const napi_value &object, uint8_t **uint8Buffer, size_t &bufferSize, + size_t &offset) { + bool isTypedArray = false; + if (napi_is_typedarray(env, object, &isTypedArray) != napi_ok || !isTypedArray) { + USB_HILOGW(MODULE_JS_NAPI, "[USB Js] JsUint8ArrayParse invalid type"); + return false; + } + + napi_typedarray_type type; + napi_value buffer; + + napi_status infoStatus = + napi_get_typedarray_info(env, object, &type, &bufferSize, (void **)uint8Buffer, &buffer, &offset); + if (infoStatus != napi_ok) { + USB_HILOGW(MODULE_JS_NAPI, "[USB Js] JsUint8ArrayParse get typedarray info failed, status: %{public}d", + infoStatus); + return false; + } + + if (type != napi_uint8_array) { + USB_HILOGW(MODULE_JS_NAPI, "[USB Js] JsUint8ArrayParse not Uint8Array type: %{public}d", type); + return false; + } + + return true; +} + +static void ProcessPromise(napi_env env, USBAsyncContext *asyncContext, napi_value result) { + if (asyncContext->deferred) { + if (asyncContext->status == napi_ok) { + napi_resolve_deferred(env, asyncContext->deferred, result); + } else { + napi_reject_deferred(env, asyncContext->deferred, result); + } + } +} + +static void Uint8ArrayToJsValue(const napi_env &env, uint8_t *uint8Buffer, size_t bufferSize, napi_value &result) +{ + uint8_t *nativeArraybuffer = new uint8_t[bufferSize]; + napi_value nativeValue = nullptr; + napi_create_arraybuffer(env, bufferSize, (void **)&nativeArraybuffer, &nativeValue); + memcpy_s(nativeArraybuffer, bufferSize, uint8Buffer, bufferSize); + napi_create_typedarray(env, napi_uint8_array, bufferSize, nativeValue, 0, &result); +} + +static void SetValueUtf8String(const napi_env& env, const char* fieldStr, const char* str, napi_value& result) +{ + napi_value value; + napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, fieldStr, value); +} + +static void SetValueInt32(const napi_env& env, const char* fieldStr, const int intValue, napi_value& result) +{ + napi_value value; + napi_create_int32(env, intValue, &value); + napi_set_named_property(env, result, fieldStr, value); +} + +static void SetValueBool(const napi_env& env, const char* fieldStr, const bool boolValue, napi_value& result) +{ + napi_value value; + napi_get_boolean(env, boolValue, &value); + napi_set_named_property(env, result, fieldStr, value); +} + +static void CtoJSUsbEndpoint(const napi_env& env, napi_value &obj, USBEndpoint &usbEndpoint){ + napi_create_object(env, &obj); + SetValueInt32(env, "address", usbEndpoint.GetAddress(), obj); + SetValueInt32(env, "attributes", usbEndpoint.GetAttributes(), obj); + SetValueInt32(env, "interval", usbEndpoint.GetInterval(), obj); + SetValueInt32(env, "maxPacketSize", usbEndpoint.GetMaxPacketSize(), obj); + SetValueInt32(env, "direction", usbEndpoint.GetDirection(), obj); + SetValueInt32(env, "number", usbEndpoint.GetEndpointNumber(), obj); + SetValueInt32(env, "type", usbEndpoint.GetType(), obj); + SetValueInt32(env, "interfaceId", usbEndpoint.GetInterfaceId(), obj); +} + +static void CtoJSUsbInterface(const napi_env& env, napi_value &obj, UsbInterface &usbInterface){ + napi_create_object(env, &obj); + SetValueInt32(env, "id", usbInterface.GetId(), obj); + SetValueInt32(env, "protocol", usbInterface.GetProtocol(), obj); + SetValueInt32(env, "clazz", usbInterface.GetClass(), obj); + SetValueInt32(env, "subclass", usbInterface.GetSubClass(), obj); + SetValueInt32(env, "alternateSetting", usbInterface.GetAlternateSetting(), obj); + SetValueUtf8String(env, "name", usbInterface.GetName().c_str(), obj); + napi_value arr; + napi_create_array(env, &arr); + for (int32_t i = 0; i < usbInterface.GetEndpointCount(); ++i) + { + USBEndpoint usbEndpoint; + usbInterface.GetEndpoint(i, usbEndpoint); + napi_value obj; + CtoJSUsbEndpoint(env, obj, usbEndpoint); + napi_set_element(env, arr, i, obj); + } + napi_set_named_property(env, obj, "endpoints", arr); +} + +static void CtoJSUsbConfig(const napi_env& env, napi_value &obj, USBConfig &usbConfig){ + napi_create_object(env, &obj); + SetValueInt32(env, "id", usbConfig.GetId(), obj); + SetValueInt32(env, "attributes", usbConfig.GetAttributes(), obj); + SetValueBool(env, "isRemoteWakeup", usbConfig.IsRemoteWakeup(),obj); + SetValueBool(env, "isSelfPowered", usbConfig.IsSelfPowered(),obj); + SetValueInt32(env, "maxPower", usbConfig.GetMaxPower(), obj); + SetValueUtf8String(env, "name", usbConfig.GetName().c_str(), obj); + napi_value arr; + napi_create_array(env, &arr); + for (int32_t i = 0; i < usbConfig.GetInterfaceCount(); ++i) + { + UsbInterface usbInterface; + usbConfig.GetInterface(i, usbInterface); + napi_value obj; + CtoJSUsbInterface(env, obj, usbInterface); + napi_set_element(env, arr, i, obj); + } + napi_set_named_property(env, obj, "interfaces", arr); +} + +static void CtoJSUsbDevice(const napi_env& env, napi_value &obj, UsbDevice *usbDevice){ + napi_create_object(env, &obj); + SetValueUtf8String(env, "name", usbDevice->GetName().c_str(), obj); + SetValueUtf8String(env, "serial", usbDevice->GetmSerial().c_str(), obj); + SetValueUtf8String(env, "manufacturerName", usbDevice->GetManufacturerName().c_str(), obj); + SetValueUtf8String(env, "productName", usbDevice->GetProductName().c_str(), obj); + SetValueUtf8String(env, "version", usbDevice->GetVersion().c_str(), obj); + SetValueInt32(env, "vendorId", usbDevice->GetVendorId(), obj); + SetValueInt32(env, "productId", usbDevice->GetProductId(), obj); + SetValueInt32(env, "clazz", usbDevice->GetClass(), obj); + SetValueInt32(env, "subclass", usbDevice->GetSubclass(), obj); + SetValueInt32(env, "protocol", usbDevice->GetProtocol(), obj); + SetValueInt32(env, "devAddress", usbDevice->GetDevAddr(), obj); + SetValueInt32(env, "busNum", usbDevice->GetBusNum(), obj); + napi_value arr; + napi_create_array(env, &arr); + for (int32_t i = 0; i < usbDevice->GetConfigCount(); ++i) + { + USBConfig usbConfig; + usbDevice->GetConfig(i, usbConfig); + napi_value obj; + CtoJSUsbConfig(env, obj, usbConfig); + napi_set_element(env, arr, i, obj); + } + napi_set_named_property(env, obj, "configs", arr); +} + +static UsbSrvClient &g_usbClient = UsbSrvClient::GetInstance(); + + +/* ============================================= Creators ============================================= */ + +static void CreateUsbDevicePipe(napi_env env, napi_value &obj, const USBDevicePipe &pip) +{ + USB_HILOGD(MODULE_JS_NAPI, "CreateUsbDevicePipe call fd"); + SetValueInt32(env, "fd", pip.GetFd(),obj); + SetValueInt32(env, "busNum", pip.GetBusNum(), obj); + SetValueInt32(env, "devAddress", pip.GetDevAddr(), obj); + USB_HILOGD(MODULE_JS_NAPI, "CreateUsbDevicePipe call serial"); + SetValueUtf8String(env, "serial", pip.GetSerial().c_str(), obj); + USB_HILOGD(MODULE_JS_NAPI, "CreateUsbDevicePipe call end serial"); +} + +//c to js +static void CreateEndpointsObj(napi_env env, napi_value &endpointObj, const USBEndpoint &ep) +{ + SetValueInt32(env, "address", ep.GetAddress(), endpointObj); + SetValueInt32(env, "attributes", ep.GetAttributes(), endpointObj); + SetValueInt32(env, "interval", ep.GetInterval(), endpointObj); + SetValueInt32(env, "maxPacketSize", ep.GetMaxPacketSize(), endpointObj); + SetValueInt32(env, "direction", ep.GetDirection(), endpointObj); + SetValueInt32(env, "number", ep.GetNumber(), endpointObj); + SetValueInt32(env, "type", ep.GetType(), endpointObj); + SetValueInt32(env, "interfaceId", ep.GetInterfaceId(), endpointObj); +} + +static void CreateUsbRequest(napi_env env, napi_value &reqObj, UsbRequest &req) +{ + USB_HILOGD(MODULE_JS_NAPI, "CreateUsbRequest call pip"); + napi_value pipObj; + napi_create_object(env, &pipObj); + CreateUsbDevicePipe(env, pipObj, req.GetPipe()); + napi_set_named_property(env, reqObj, "pipe", pipObj); + + USB_HILOGD(MODULE_JS_NAPI, "CreateUsbRequest call endpoint"); + napi_value epObj; + napi_create_object(env, &epObj); + const USBEndpoint &ep = req.GetEndpoint(); + CreateEndpointsObj(env, epObj, ep); + napi_set_named_property(env, reqObj, "endpoint", epObj); + + USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call buffer"); + napi_value bufferObj; + uint8_t *buffer = req.GetBuffer(); + size_t length = req.GetLength(); + Uint8ArrayToJsValue(env, buffer, length, bufferObj); + napi_set_named_property(env, reqObj, "buffer", bufferObj); + + USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call clientData"); + napi_value clientObj; + uint32_t clientLength = req.GetClientLength(); + void *data = req.GetClientData(); + Uint8ArrayToJsValue(env, (uint8_t *)data, clientLength, clientObj); + napi_set_named_property(env, reqObj, "clientData", clientObj); + USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call end"); +} + + +/* ============================================= Parsers ============================================= */ + +//js to c +static void ParseEndpointObj(napi_env env, napi_value endpointObj, USBEndpoint &ep) +{ + int32_t address, attributes, interval, maxPacketSize, interfaceId = 0; + JsObjectToInt(env, endpointObj, "address", address); + JsObjectToInt(env, endpointObj, "attributes", attributes); + JsObjectToInt(env, endpointObj, "interval", interval); + JsObjectToInt(env, endpointObj, "maxPacketSize", maxPacketSize); + JsObjectToInt(env, endpointObj, "interfaceId", interfaceId); + ep = USBEndpoint(address, attributes, interval, maxPacketSize); + ep.SetInterfaceId(interfaceId); +} + +static bool ParseEndpointsObjs(napi_env env, napi_value interfaceObj, std::vector &eps) +{ + // Get the array. + bool result = false; + napi_status status = napi_is_array(env, interfaceObj, &result); + if (result && status == napi_ok) { + napi_value endpointsObjs; + bool isGetObjSuccess = JsObjectGetProperty(env, interfaceObj, "endpoints", &endpointsObjs); + if (!isGetObjSuccess) { + return false; + } + + uint32_t endpointCount = 0; + status = napi_get_array_length(env, endpointsObjs, &endpointCount); + + for(uint32_t k = 0; k < endpointCount; ++k) { + napi_value endpointObj; + napi_status status = napi_get_element(env, endpointsObjs, k, &endpointObj); + if(status != napi_ok) { + return false; + } + USBEndpoint ep; + ParseEndpointObj(env, endpointObj, ep); + eps.push_back(ep); + } + } + + return false; +} + +struct PipeControlParam{ + int32_t request; + int32_t target; + int32_t reqType; + int32_t directon; + int32_t value; + int32_t index; + uint8_t *data; + size_t dataLength; +}; + +static void ParsePipeControlParam(napi_env env, napi_value jsObj, PipeControlParam &controlParam) { + int32_t request, target, reqType, directon, value, index = 0; + JsObjectToInt(env, jsObj, "request", request); + JsObjectToInt(env, jsObj, "target", target); + JsObjectToInt(env, jsObj, "reqType", reqType); + JsObjectToInt(env, jsObj, "directon", directon); + JsObjectToInt(env, jsObj, "value", value); + JsObjectToInt(env, jsObj, "index", index); + + napi_value dataValue; + JsObjectGetProperty(env, jsObj, "data", &dataValue); + + uint8_t *data = nullptr; + size_t dataLength = 0; + size_t offset = 0; + JsUint8ArrayParse(env, dataValue, &data, dataLength, offset); + controlParam.request = request; + controlParam.target = target; + controlParam.reqType = reqType; + controlParam.directon = directon; + controlParam.value = value; + controlParam.index = index; + controlParam.data = data; + controlParam.dataLength = dataLength; +} + +static void ParseInterfaceObj(napi_env env, napi_value interfaceObj, UsbInterface &interface) +{ + int32_t mId, mProtocol, mClass, mSubClass, mAlternateSetting = 0; + std::string mName; + JsObjectToInt(env, interfaceObj, "id", mId); + JsObjectToInt(env, interfaceObj, "protocol", mProtocol); + JsObjectToInt(env, interfaceObj, "clazz", mClass); + JsObjectToInt(env, interfaceObj, "subclass", mSubClass); + JsObjectToInt(env, interfaceObj, "alternateSetting", mAlternateSetting); + JsObjectToString(env, interfaceObj, "name", 32, mName); + std::vector eps; + ParseEndpointsObjs(env, interfaceObj, eps); + interface = UsbInterface(mId, mProtocol, mClass, mSubClass, mAlternateSetting, mName, eps); +} + +static bool ParseInterfacesObjs(napi_env env, napi_value configObj, std::vector &interfaces) { + bool result = false; + napi_status status = napi_is_array(env, configObj, &result); + if (result && status == napi_ok) { + napi_value interfacesObjs; + bool isGetObjSuccess = JsObjectGetProperty(env, configObj, "interfaces", &interfacesObjs); + if (!isGetObjSuccess) { + return false; + } + uint32_t interfaceCount = 0; + status = napi_get_array_length(env, interfacesObjs, &interfaceCount); + for (uint32_t i = 0; i < interfaceCount; ++i) { + napi_value interfaceObj; + napi_status status = napi_get_element(env, interfacesObjs, i, &interfaceObj); + if (status != napi_ok) { + return false; + } + UsbInterface interface; + ParseInterfaceObj(env, interfaceObj, interface); + interfaces.push_back(interface); + } + } + + return false; +} + +static void ParseConfigObj(napi_env env, napi_value configObj, USBConfig &config) +{ + int32_t id, attributes, maxPower = 0; + std::string name; + JsObjectToInt(env, configObj, "id", id); + JsObjectToInt(env, configObj, "attributes", attributes); + JsObjectToInt(env, configObj, "maxPower", maxPower); + JsObjectToString(env, configObj, "name", 32, name); + + std::vector interfaces; + ParseInterfacesObjs(env, configObj, interfaces); + config = USBConfig(id, attributes, name, maxPower, interfaces); +} + +static bool ParseConfigsObjs(napi_env env, napi_value deviceObj, std::vector &configs) +{ + napi_value configsObj; + JsObjectGetProperty(env, deviceObj, "configs", &configsObj); + napi_valuetype valueType; + napi_typeof(env, configsObj, &valueType); + // NAPI_ASSERT_RETURN_VOID(env, valueType == napi_object, "Wrong argument type. String expected."); + + uint32_t configCount = 0; + napi_get_array_length(env, configsObj, &configCount); + for(uint32_t i = 0; i < configCount; ++i) { + napi_value configObj; + napi_get_element(env, configsObj, i, &configObj); + USBConfig config; + ParseConfigObj(env, configObj, config); + configs.push_back(config); + } + return true; +} + +static void ParseUsbDevicePipe(napi_env env, napi_value &obj, USBDevicePipe &pip) +{ + napi_valuetype valueType; + napi_typeof(env, obj, &valueType); + NAPI_ASSERT_RETURN_VOID(env, valueType == napi_object, "Wrong argument type. String expected."); + + int32_t fd = 0; + JsObjectToInt(env, obj, "fd", fd); + pip.SetFd(fd); + int32_t busNum = 0; + JsObjectToInt(env, obj, "busNum", busNum); + pip.SetBusNum((uint8_t)busNum); + int32_t devAddr = 0; + JsObjectToInt(env, obj, "devAddress", devAddr); + pip.SetDevAddr((uint8_t)devAddr); + std::string serial; + JsObjectToString(env, obj, "serial", 32, serial); + pip.SetSerial(serial); +} + +static void ParseUsbRequest(napi_env env, napi_value &obj, UsbRequest &req) { + napi_valuetype valueType; + napi_typeof(env, obj, &valueType); + NAPI_ASSERT_RETURN_VOID(env, valueType == napi_object, "Wrong argument type. String expected."); + +USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call pip"); + USBDevicePipe pip; + napi_value pipObj; + bool hasPipe = JsObjectGetProperty(env, obj, "pipe", &pipObj); + if (hasPipe) { + ParseUsbDevicePipe(env, pipObj, pip); + req.SetPipe(pip); + } + +USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call endpoint"); + USBEndpoint ep; + napi_value epObj; + bool hasEndpoint = JsObjectGetProperty(env, obj, "endpoint", &epObj); + if (hasEndpoint) { + ParseEndpointObj(env, epObj, ep); + req.SetEndpoint(ep); + } + +USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call buffer"); + napi_value bufferObj; + bool hasBuffer = JsObjectGetProperty(env, obj, "buffer", &bufferObj); + if (hasBuffer) { + void *bufferData = nullptr; + uint32_t bufferLength = 0; + napi_get_arraybuffer_info(env, bufferObj, &bufferData, &bufferLength); + req.SetBuffer((uint8_t *)bufferData); + req.SetLength(bufferLength); + } + +USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call clientData"); + napi_value clientObj; + bool hasData = JsObjectGetProperty(env, obj, "clientData", &clientObj); + if (hasData) { + uint8_t *clientData = nullptr; + size_t clientLength = 0; + size_t offset = 0; + JsUint8ArrayParse(env, clientObj, &clientData, clientLength, offset); + req.SetClientData(clientData, clientLength); + } + USB_HILOGD(MODULE_JS_NAPI, "ParseUsbRequest call end"); +} + +/* ============================================= Usb Core ============================================= */ + +static napi_value CoreGetDevices(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + std::vector deviceList; + int32_t ret = g_usbClient.GetDevices(deviceList); + napi_value result; + if (ret != ERR_OK) { + napi_get_undefined(env, &result); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, get device faild ret : %{public}d", __func__, ret); + return result; + } + + napi_create_array(env, &result); + int32_t i =0; + for(auto &ent1 : deviceList){ + napi_value element; + napi_create_object(env, &element); + napi_value device; + CtoJSUsbDevice(env, device, &ent1); + napi_set_element(env, result, i, device); + ++i; + } + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value CoreConnectDevice(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_value deviceObj = argv[0]; + napi_valuetype type; + napi_typeof(env, deviceObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + + UsbDevice dev; + std::string mName, mManufacturerName, mProductName, mVersion; + int32_t devAddr, busNum, mVendorId, mProductId, mClass, mSubclass, mProtocol = 0; + JsObjectToString(env, deviceObj, "name", 32, mName); /* manufacturer max length is 32 + '\0' */ + JsObjectToString(env, deviceObj, "manufacturerName", 32, mManufacturerName); /* manufacturer max length is 32 + '\0' */ + JsObjectToString(env, deviceObj, "productName", 32, mProductName); + JsObjectToString(env, deviceObj, "version", 32, mVersion); + JsObjectToInt(env, deviceObj, "devAddress", devAddr); + JsObjectToInt(env, deviceObj, "busNum", busNum); + JsObjectToInt(env, deviceObj, "vendorId", mVendorId); + JsObjectToInt(env, deviceObj, "productId", mProductId); + JsObjectToInt(env, deviceObj, "clazz", mClass); + JsObjectToInt(env, deviceObj, "subclass", mSubclass); + JsObjectToInt(env, deviceObj, "protocol", mProtocol); + std::vector configs; + ParseConfigsObjs(env, deviceObj, configs); + dev = UsbDevice(mName,mManufacturerName,mProductName,mVersion,devAddr,busNum,mVendorId,mProductId,mClass,mSubclass, + mProtocol, configs); + USBDevicePipe pip; + int32_t ret = g_usbClient.OpenDevice(dev, pip); + napi_value pipObj; + if (ret != ERR_OK) { + napi_get_undefined(env, &pipObj); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, error result code %{public}d,takes : %{public}d ms", __func__, + ret, (end - begin) / MSEC_TIME); + return pipObj; + } + + napi_create_object(env, &pipObj); + SetValueInt32(env, "fd", pip.GetFd(), pipObj); + SetValueInt32(env, "busNum", pip.GetBusNum(), pipObj); + SetValueInt32(env, "devAddress", pip.GetDevAddr(), pipObj); + SetValueUtf8String(env, "serial", pip.GetSerial().c_str(), pipObj); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return pipObj; +} + +static napi_value CoreHasRight(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value args[1] = {0}; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[0], &type)); + + NAPI_ASSERT(env, type == napi_string, "Wrong argument type. String expected."); + + std::string deviceName; + JsValueToString(env, args[0], 256, deviceName); + int32_t result = g_usbClient.HasRight(deviceName); + USB_HILOGD(MODULE_JS_NAPI, "client called result %{public}d", result); + + napi_value napiValue = nullptr; + napi_get_boolean(env, result == ERR_OK, &napiValue); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return napiValue; +} + +static napi_value CoreRequestRight(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value args[1] = {0}; + napi_value jsthis; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[0], &type)); + NAPI_ASSERT(env, type == napi_string, "Wrong argument type. String expected."); + std::string deviceName; + JsValueToString(env, args[0], 256, deviceName); + + auto *asyncContext = new USBRightAsyncContext(); + asyncContext->env = env; + asyncContext->deviceName = deviceName; + + napi_value result = nullptr; + napi_create_promise(env, &asyncContext->deferred, &result); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "RequestRight", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + int32_t beginAsync = clock(); + USBRightAsyncContext *asyncContext = (USBRightAsyncContext *)data; + int32_t ret = g_usbClient.RequestRight(asyncContext->deviceName); + if (ret == ERR_OK) { + asyncContext->status = napi_ok; + } else { + asyncContext->status = napi_generic_failure; + } + int32_t endAsync = clock(); + USB_HILOGD(MODULE_JS_NAPI, " %{public}s async work finished, takes : %{public}d ms", __func__, + (endAsync - beginAsync) / MSEC_TIME); + }, + [](napi_env env, napi_status status, void *data) { + USBRightAsyncContext *asyncContext = (USBRightAsyncContext*)data; + napi_value queryResult = nullptr; + napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult); + + ProcessPromise(env, asyncContext, queryResult); + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void *)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value CoreUsbFunctionsFromString(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + napi_typeof(env, argv[0], &type); + NAPI_ASSERT(env, type == napi_string, "Wrong argument type. String expected."); + + // get value string argument of napi converted. + std::string funcs; + JsValueToString(env, argv[0], 256, funcs); + + int32_t numFuncs = g_usbClient.UsbFunctionsFromString(funcs); + + napi_value result; + napi_create_int32(env, numFuncs, &result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value CoreUsbFunctionsToString(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + napi_typeof(env, argv[0], &type); + NAPI_ASSERT(env, type == napi_number, "Wrong argument type. String expected."); + + int32_t funcs; + napi_get_value_int32(env, argv[0], &funcs); + std::string strFuncs = g_usbClient.UsbFunctionsToString(funcs); + + napi_value result; + napi_create_string_utf8(env, strFuncs.c_str(), NAPI_AUTO_LENGTH, &result); + + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value CoreSetCurrentFunctions(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + napi_typeof(env, argv[0], &type); + NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Number expected."); + + int32_t funcs = 0; + napi_get_value_int32(env, argv[0], &funcs); + + auto *asyncContext = new USBFunctionAsyncContext(); + asyncContext->env = env; + asyncContext->functions = funcs; + napi_value result = nullptr; + napi_create_promise(env, &asyncContext->deferred, &result); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "SetCurrentFunctions", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + int32_t beginAsync = clock(); + USBFunctionAsyncContext *asyncContext = (USBFunctionAsyncContext*)data; + bool ret = g_usbClient.SetCurrentFunctions(asyncContext->functions); + if (ret == ERR_OK) { + asyncContext->status = napi_ok; + } else { + asyncContext->status = napi_generic_failure; + } + int32_t endAsync = clock(); + USB_HILOGD(MODULE_JS_NAPI, " %{public}s async work finished, takes : %{public}d ms", __func__, + (endAsync - beginAsync) / MSEC_TIME); + }, + [](napi_env env, napi_status status, void *data) { + USBRightAsyncContext *asyncContext = (USBRightAsyncContext*)data; + napi_value queryResult = nullptr; + napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult); + + ProcessPromise(env, asyncContext, queryResult); + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void*)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value CoreGetCurrentFunctions(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + int32_t cfuncs = g_usbClient.GetCurrentFunctions(); + napi_value result; + napi_create_int32(env, cfuncs, &result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value CoreGetPorts(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + + std::vector ports; + + int32_t ret = g_usbClient.GetPorts(ports); + napi_value result; + if (ret != ERR_OK) { + napi_get_undefined(env, &result); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, get ports faild ret : %{public}d", __func__, ret); + return result; + } + + napi_create_array(env, &result); + for (uint32_t i = 0; i < ports.size(); ++i) { + napi_value port; + napi_create_object(env, &port); + SetValueInt32(env, "id", ports[i]->id, port); + SetValueInt32(env, "supportedModes", ports[i]->supportedModes, port); + napi_value usbPortStatus; + napi_create_object(env, &usbPortStatus); + SetValueInt32(env, "currentMode", ports[i]->usbPortStatus->currentMode, usbPortStatus); + SetValueInt32(env, "currentPowerRole", ports[i]->usbPortStatus->currentPowerRole, usbPortStatus); + SetValueInt32(env, "currentDataRole", ports[i]->usbPortStatus->currentDataRole, usbPortStatus); + napi_set_named_property(env, port, "status", usbPortStatus); + napi_set_element(env, result, i, port); + } + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + + +/* ============================================= Usb Port ============================================= */ + +static napi_value PortGetSupportedModes(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value args[1] = {0}; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[0], &type)); + NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Number expected."); + + int32_t id = 0; + napi_get_value_int32(env, args[0], &id); + int32_t result = g_usbClient.GetSupportedModes(id); + napi_value napiValue = nullptr; + NAPI_CALL(env, napi_create_int32(env, result, &napiValue)); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, result : %{public}d, takes : %{public}d ms", __func__, result, + (end - begin) / MSEC_TIME); + return napiValue; +} + +static napi_value PortSetPortRole(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 3; + napi_value args[3] = { 0 }; + napi_value jsthis; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, args, &jsthis, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_valuetype type; + NAPI_CALL(env, napi_typeof(env, args[0], &type)); + NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Number expected."); + NAPI_CALL(env, napi_typeof(env, args[1], &type)); + NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Number expected."); + NAPI_CALL(env, napi_typeof(env, args[2], &type)); + NAPI_ASSERT(env, type == napi_number, "Wrong argument type. Number expected."); + + int32_t id, powerRole, dataRole = 0; + napi_get_value_int32(env, args[0], &id); + napi_get_value_int32(env, args[1], &powerRole); + napi_get_value_int32(env, args[2], &dataRole); + + + auto *asyncContext = new USBPortRoleAsyncContext(); + asyncContext->env = env; + asyncContext->portId = id; + asyncContext->dataRole = dataRole; + asyncContext->powerRole = powerRole; + + napi_value result = nullptr; + napi_create_promise(env, &asyncContext->deferred, &result); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "PortSetPortRole", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + int32_t beginAsync = clock(); + USBPortRoleAsyncContext *asyncContext = (USBPortRoleAsyncContext*)data; + int32_t ret = + g_usbClient.SetPortRole(asyncContext->portId, asyncContext->powerRole, asyncContext->dataRole); + if (ret == ERR_OK) { + asyncContext->status = napi_ok; + } else { + asyncContext->status = napi_generic_failure; + } + int32_t endAsync = clock(); + USB_HILOGD(MODULE_JS_NAPI, " %{public}s async work finished, takes : %{public}d ms", __func__, + (endAsync - beginAsync) / MSEC_TIME); + }, + [](napi_env env, napi_status status, void *data) { + USBRightAsyncContext *asyncContext = (USBRightAsyncContext*)data; + napi_value queryResult = nullptr; + + napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult); + + ProcessPromise(env, asyncContext, queryResult); + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void*)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value PipeClaimInterface(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 3; + napi_value argv[3] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 2), "Failed to get cb info"); + + napi_value obj = argv[0]; + napi_valuetype type; + napi_typeof(env, obj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + + USBDevicePipe pip; + ParseUsbDevicePipe(env, obj, pip); + + UsbInterface interface; + napi_value obj2 = argv[1]; + napi_typeof(env, obj2, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + ParseInterfaceObj(env, obj2, interface); + + bool isForce = false; + if (argc >= 3) { + napi_typeof(env, argv[2], &type); + NAPI_ASSERT(env, type == napi_boolean, "Wrong argument type. Object expected."); + napi_get_value_bool(env, argv[2], &isForce); + } + + int32_t ret = pip.ClaimInterface(interface, isForce); + USB_HILOGD(MODULE_JS_NAPI, "pip call ClaimInterface ret: %{public}d", ret); + napi_value result; + napi_get_boolean(env, ret == ERR_OK, &result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}d ms", __func__, ret, + (end - begin) / MSEC_TIME); + return result; +} + +static napi_value PipeReleaseInterface(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 2; + napi_value argv[2] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 2), "Failed to get cb info"); + + napi_value obj = argv[0]; + napi_valuetype type; + napi_typeof(env, obj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + + USBDevicePipe pip; + ParseUsbDevicePipe(env, obj, pip); + + UsbInterface interface; + napi_value obj2 = argv[1]; + ParseInterfaceObj(env, obj2, interface); + int32_t ret = pip.ReleaseInterface(interface); + USB_HILOGD(MODULE_JS_NAPI, "pip call PipeReleaseInterface ret: %{public}d", ret); + napi_value result; + napi_get_boolean(env, ret == ERR_OK, &result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}d ms", __func__, ret, + (end - begin) / MSEC_TIME); + return result; +} + +// static napi_value SetConfiguration(napi_env env, napi_callback_info info) +// { +// size_t argc = 1; +// napi_value argv[1] = { 0 }; +// napi_value thiz; +// void *data = nullptr; + +// napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); +// NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); +// napi_value obj = argv[0]; +// napi_valuetype type; +// napi_typeof(env, obj, &type); +// NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + +// USBConfig config; +// ParseConfigObj(env, obj, config); +// bool ret = g_usbClient.SetConfiguration(config); +// napi_value result; +// napi_get_boolean(env, ret, &result); +// USB_HILOGD(MODULE_JS_NAPI, "result %{public}d", 0); +// return result; +// } + +static napi_value PipeSetInterface(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 2; + napi_value argv[2] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 2), "Failed to get cb info"); + + napi_value pipeObj = argv[0]; + napi_valuetype type; + napi_typeof(env, pipeObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + + USBDevicePipe pip; + ParseUsbDevicePipe(env, pipeObj, pip); + + napi_value interfaceObj = argv[1]; + napi_typeof(env, interfaceObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + + UsbInterface interface; + ParseInterfaceObj(env, interfaceObj, interface); + bool ret = pip.SetInterface(interface); + napi_value result; + napi_get_boolean(env, ret, &result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}d ms", __func__, ret, + (end - begin) / MSEC_TIME); + return result; +} + +static napi_value PipeGetRawDescriptors(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + napi_value obj = argv[0]; + napi_valuetype type; + napi_typeof(env, obj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + + USBDevicePipe pip; + ParseUsbDevicePipe(env, obj, pip); + + // pipe.getRawDescriptiors + // int32_t ret = g_usbClient.GetRawDescriptors(buffer2, length2); + napi_value result; + napi_create_int32(env, -1, &result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}d ms", __func__, -1, + (end - begin) / MSEC_TIME); + return result; +} + +static napi_value PipeGetFileDescriptor(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + int32_t fd = g_usbClient.GetFileDescriptor(); + napi_value result; + SetValueInt32(env, "fd", fd, result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value PipeControlTransfer(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + + size_t argc = 3; + napi_value argv[3] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 2), "Failed to get cb info"); + + // pipe + napi_value pipeObj = argv[0]; + napi_valuetype type; + napi_typeof(env, pipeObj, &type); + NAPI_ASSERT(env, type == napi_object, "argv[0] wrong argument type, object expected."); + USBDevicePipe pipe; + ParseUsbDevicePipe(env, pipeObj, pipe); + + // control params + PipeControlParam controlParam; + ParsePipeControlParam(env, argv[1], controlParam); + + // timeout + int32_t timeout = 0; + if (argc == 3) { + napi_get_value_int32(env, argv[2], &timeout); + } + + auto *asyncContext = new USBControlTransferAsyncContext(); + asyncContext->env = env; + asyncContext->pipe = pipe; + asyncContext->request = controlParam.request; + asyncContext->target = controlParam.target; + asyncContext->reqType = controlParam.reqType; + asyncContext->directon = controlParam.directon; + asyncContext->value = controlParam.value; + asyncContext->index = controlParam.index; + asyncContext->buffer = controlParam.data; + asyncContext->bufferLength = controlParam.dataLength; + asyncContext->timeout = timeout; + napi_value result = nullptr; + napi_create_promise(env, &asyncContext->deferred, &result); + + napi_value resource = nullptr; + napi_create_string_utf8(env, "PipeControlTransfer", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + int32_t beginAsync = clock(); + USBControlTransferAsyncContext *asyncContext = (USBControlTransferAsyncContext*)data; + int32_t ret = asyncContext->pipe.ControlTransfer( + asyncContext->reqType, asyncContext->request, asyncContext->value, asyncContext->index, + asyncContext->buffer, asyncContext->bufferLength, asyncContext->timeout); + if (ret == ERR_OK) { + asyncContext->status = napi_ok; + } else { + asyncContext->status = napi_generic_failure; + } + int32_t endAsync = clock(); + USB_HILOGD(MODULE_JS_NAPI, " %{public}s async work finished, takes : %{public}d ms", __func__, + (endAsync - beginAsync) / MSEC_TIME); + }, + [](napi_env env, napi_status status, void *data) { + USBControlTransferAsyncContext *asyncContext = (USBControlTransferAsyncContext*)data; + napi_value queryResult = nullptr; + + if (asyncContext->status == napi_ok) { + // Uint8ArrayToJsValue(env, asyncContext->buffer, asyncContext->bufferLength, queryResult); + napi_create_int32(env, asyncContext->bufferLength, &queryResult); + } else { + napi_value message = nullptr; + napi_create_string_utf8(env, "PipeControlTransfer failed", NAPI_AUTO_LENGTH, &message); + napi_create_error(env, nullptr, message, &queryResult); + } + ProcessPromise(env, asyncContext, queryResult); + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void*)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value PipeBulkTransfer(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 4; + napi_value argv[4] = {0}; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 3), "Failed to get cb info"); + + napi_valuetype type; + + USBDevicePipe pip; + napi_typeof(env, argv[0], &type); + NAPI_ASSERT(env, type == napi_object, "argv[0] wrong argument type, object expected"); + ParseUsbDevicePipe(env, argv[0], pip); + + USBEndpoint ep; + napi_typeof(env, argv[1], &type); + NAPI_ASSERT(env, type == napi_object, "argv[1] Wrong argument type. Object expected."); + ParseEndpointObj(env, argv[1], ep); + + uint8_t *buffer = nullptr; + size_t offset; + size_t bufferSize; + bool hasBuffer = JsUint8ArrayParse(env, argv[2], &buffer, bufferSize, offset); + int32_t timeout = 0; + if (argc > 3) { + napi_get_value_int32(env, argv[3], &timeout); + } + + auto *asyncContext = new USBBulkTransferAsyncContext(); + asyncContext->env = env; + asyncContext->pipe = pip; + asyncContext->endpoint = ep; + asyncContext->buffer = buffer; + asyncContext->bufferLength = bufferSize; + asyncContext->timeout = timeout; + napi_value result = nullptr; + napi_create_promise(env, &asyncContext->deferred, &result); + if (!hasBuffer) { + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s: failed buffer is null", __func__); + asyncContext->status = napi_invalid_arg; + napi_value queryResult = nullptr; + napi_value message = nullptr; + napi_create_string_utf8(env, "Bulk transfer invalid arg", NAPI_AUTO_LENGTH, &message); + napi_create_error(env, nullptr, message, &queryResult); + ProcessPromise(env, asyncContext, queryResult); + return result; + } + + napi_value resource = nullptr; + napi_create_string_utf8(env, "PipeBulkTransfer", NAPI_AUTO_LENGTH, &resource); + status = napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + int32_t beginAsync = clock(); + USBBulkTransferAsyncContext *asyncContext = (USBBulkTransferAsyncContext*)data; + int32_t ret = asyncContext->pipe.BulkTransfer(asyncContext->endpoint, asyncContext->buffer, + asyncContext->bufferLength, asyncContext->timeout); + USB_HILOGD(MODULE_JS_NAPI, "call pipe result %{public}d", ret); + if (ret == ERR_OK) { + asyncContext->status = napi_ok; + } else { + asyncContext->status = napi_generic_failure; + } + int32_t endAsync = clock(); + USB_HILOGD(MODULE_JS_NAPI, " %{public}s async work finished, takes : %{public}d ms", __func__, + (endAsync - beginAsync) / MSEC_TIME); + }, + [](napi_env env, napi_status status, void *data) { + USBBulkTransferAsyncContext *asyncContext = (USBBulkTransferAsyncContext*)data; + napi_value queryResult = nullptr; + if (asyncContext->status == napi_ok) { + // Uint8ArrayToJsValue(env, asyncContext->buffer, asyncContext->bufferLength, queryResult); + napi_create_int32(env, asyncContext->bufferLength, &queryResult); + + } else { + napi_value message = nullptr; + napi_create_string_utf8(env, "PipeBulkTransfer failed", NAPI_AUTO_LENGTH, &message); + napi_create_error(env, nullptr, message, &queryResult); + } + ProcessPromise(env, asyncContext, queryResult); + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void*)asyncContext, &asyncContext->work); + int32_t end = clock(); + if (status != napi_ok) { + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, create async work failed, takes : %{public}d ms", __func__, + (end - begin) / MSEC_TIME); + return result; + } + napi_queue_async_work(env, asyncContext->work); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}d ms", __func__, (end - begin) / MSEC_TIME); + return result; +} + +static void CreateUsbDevicePipe(napi_env env, napi_value &obj, const USBDevicePipe &pip); +static napi_value PipeClose(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + int32_t begin = clock(); + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_value obj = argv[0]; + napi_valuetype type; + napi_typeof(env, obj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + + USBDevicePipe pip; + ParseUsbDevicePipe(env, obj, pip); + int32_t ret = pip.Close(); + napi_value result; + napi_create_int32(env, ret, &result); + int32_t end = clock(); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, result %{public}d, takes : %{public}d ms", __func__, ret, (end - begin) / MSEC_TIME); + return result; +} + +static napi_value PipeRequestWait(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + size_t argc = 2; + napi_value argv[2] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + // get pipe + napi_value obj = argv[0]; + napi_valuetype type; + napi_typeof(env, obj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + USBDevicePipe pipe; + CreateUsbDevicePipe(env, obj, pipe); + + int64_t timeout = 0; + // get timeout or default 0 + if (argc >= 2) { + napi_get_value_int64(env, argv[1], &timeout); + } + + UsbRequest req; + int32_t requestResult = g_usbClient.PipeRequestWait(pipe, timeout, req); + USB_HILOGD(MODULE_JS_NAPI, "usbClient called result %{public}d", requestResult); + napi_value reqObj; + + if (requestResult != ERR_OK) { + napi_get_undefined(env, &reqObj); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, faild ret : %{public}d", __func__, requestResult); + return reqObj; + } + + napi_create_object(env, &reqObj); + CreateUsbRequest(env, reqObj, req); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s", __func__); + return reqObj; +} + +static napi_value RequestInitialize(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + + size_t argc = 2; + napi_value argv[2] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 2), "Failed to get cb info"); + + napi_valuetype type; + napi_value pipeObj = argv[0]; + napi_typeof(env, pipeObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + USBDevicePipe pipe; + ParseUsbDevicePipe(env, pipeObj, pipe); + + napi_value endpointObj = argv[1]; + napi_typeof(env, endpointObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + USBEndpoint endpoint; + ParseEndpointObj(env, endpointObj, endpoint); + + UsbRequest req; + bool ret = req.Initialize(pipe, endpoint); + USB_HILOGD(MODULE_JS_NAPI, "UsbRequest Initialize result %{public}d", ret); + + napi_value result; + napi_create_object(env, &result); + CreateUsbRequest(env, result, req); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s", __func__); + return result; +} + +static napi_value RequestFree(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_value reqObj = argv[0]; + napi_valuetype type; + napi_typeof(env, reqObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + UsbRequest req; + ParseUsbRequest(env, reqObj, req); + + int32_t ret = g_usbClient.RequestFree(req); + napi_value result; + napi_get_boolean(env, ret == ERR_OK, &result); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, result %{public}d", __func__, ret); + return result; +} + +static napi_value RequestAbort(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_value reqObj = argv[0]; + napi_valuetype type; + napi_typeof(env, reqObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + UsbRequest req; + ParseUsbRequest(env, reqObj, req); + + int32_t ret = g_usbClient.RequestAbort(req); + napi_value result; + napi_get_boolean(env, ret == ERR_OK, &result); + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d", __func__, ret); + return result; +} + +static napi_value RequestSetClientData(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + + size_t argc = 2; + napi_value argv[2] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 2), "Failed to get cb info"); + + napi_value reqObj = argv[0]; + napi_valuetype type; + napi_typeof(env, reqObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + UsbRequest req; + ParseUsbRequest(env, reqObj, req); + + uint8_t *clientData = nullptr; + size_t clientLength = 0; + size_t offset = 0; + JsUint8ArrayParse(env, argv[1], &clientData, clientLength, offset); + + req.SetClientData(clientData, clientLength); + napi_value result; + napi_create_object(env, &result); + + USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s", __func__); + return result; +} + +static napi_value RequestGetClientData(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + + size_t argc = 1; + napi_value argv[1] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 1), "Failed to get cb info"); + + napi_value reqObj = argv[0]; + napi_valuetype type; + napi_typeof(env, reqObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + UsbRequest req; + ParseUsbRequest(env, reqObj, req); + + void *clientData = req.GetClientData(); + size_t clienLength = req.GetClientLength(); + + napi_value clientObj; + Uint8ArrayToJsValue(env, (uint8_t *)clientData, clienLength, clientObj); + return clientObj; +} + +static napi_value RequestQueue(napi_env env, napi_callback_info info) +{ + USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__); + size_t argc = 2; + napi_value argv[2] = { 0 }; + napi_value thiz; + void *data = nullptr; + napi_status status = napi_get_cb_info(env, info, &argc, argv, &thiz, &data); + NAPI_ASSERT(env, (status == napi_ok) && (argc >= 2), "Failed to get cb info"); + + napi_value reqObj = argv[0]; + napi_valuetype type; + napi_typeof(env, reqObj, &type); + NAPI_ASSERT(env, type == napi_object, "Wrong argument type. Object expected."); + UsbRequest req; + ParseUsbRequest(env, reqObj, req); + + uint8_t *buffer = nullptr; + size_t length = 0; + size_t offset = 0; + JsUint8ArrayParse(env, argv[1], &buffer, length, offset); + + auto *asyncContext = new USBQueueAsyncContext(); + asyncContext->env = env; + asyncContext->req = req; + asyncContext->buffer = buffer; + asyncContext->bufferLength = length; + + napi_value result = nullptr; + napi_create_promise(env, &asyncContext->deferred, &result); + napi_value resource = nullptr; + napi_create_string_utf8(env, "RequestQueue", NAPI_AUTO_LENGTH, &resource); + + napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + USBQueueAsyncContext *asyncContext = (USBQueueAsyncContext*)data; + int32_t ret = asyncContext->req.Queue(asyncContext->buffer, asyncContext->bufferLength); + USB_HILOGD(MODULE_JS_NAPI, "call request queue %{public}d", ret); + if (ret == ERR_OK) { + asyncContext->status = napi_ok; + } else { + asyncContext->status = napi_generic_failure; + } + }, + [](napi_env env, napi_status status, void *data) { + USBQueueAsyncContext *asyncContext = (USBQueueAsyncContext*)data; + napi_value queryResult = nullptr; + if (asyncContext->status == napi_ok) { + Uint8ArrayToJsValue(env, asyncContext->buffer, asyncContext->bufferLength, queryResult); + } else { + napi_value message = nullptr; + napi_create_string_utf8(env, "RequestQueue failed", NAPI_AUTO_LENGTH, &message); + napi_create_error(env, nullptr, message, &queryResult); + } + ProcessPromise(env, asyncContext, queryResult); + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void*)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); + return result; +} + +static napi_value GetVersion(napi_env env, napi_callback_info info) +{ + std::string version = g_usbClient.GetVersion(); + USB_HILOGD(MODULE_JS_NAPI, "call %{public}s, version is %{public}s", __func__, version.c_str()); + napi_value result; + napi_create_string_utf8(env, version.c_str(), NAPI_AUTO_LENGTH, &result); + return result; +} + +EXTERN_C_START +/* + * function for module exports + */ +static napi_value UsbInit(napi_env env, napi_value exports) +{ + USB_HILOGD(MODULE_JS_NAPI, "enter"); + + napi_property_descriptor desc[] = { + /* usb core */ + DECLARE_NAPI_FUNCTION("getDevices", CoreGetDevices), + DECLARE_NAPI_FUNCTION("connectDevice", CoreConnectDevice), + DECLARE_NAPI_FUNCTION("hasRight", CoreHasRight), + DECLARE_NAPI_FUNCTION("requestRight", CoreRequestRight), + DECLARE_NAPI_FUNCTION("usbFunctionsFromString", CoreUsbFunctionsFromString), + DECLARE_NAPI_FUNCTION("usbFunctionsToString", CoreUsbFunctionsToString), + DECLARE_NAPI_FUNCTION("setCurrentFunctions", CoreSetCurrentFunctions), + DECLARE_NAPI_FUNCTION("getCurrentFunctions", CoreGetCurrentFunctions), + DECLARE_NAPI_FUNCTION("getPorts", CoreGetPorts), + + /* usb port */ + DECLARE_NAPI_FUNCTION("getSupportedModes", PortGetSupportedModes), + DECLARE_NAPI_FUNCTION("setPortRoles", PortSetPortRole), + + /* usb device pipe */ + DECLARE_NAPI_FUNCTION("claimInterface", PipeClaimInterface), + DECLARE_NAPI_FUNCTION("releaseInterface", PipeReleaseInterface), + DECLARE_NAPI_FUNCTION("bulkTransfer", PipeBulkTransfer), + DECLARE_NAPI_FUNCTION("controlTransfer", PipeControlTransfer), + DECLARE_NAPI_FUNCTION("setInterface", PipeSetInterface), + DECLARE_NAPI_FUNCTION("getRawDescriptor", PipeGetRawDescriptors), + DECLARE_NAPI_FUNCTION("getFileDescriptor", PipeGetFileDescriptor), + DECLARE_NAPI_FUNCTION("requestWait", PipeRequestWait), + DECLARE_NAPI_FUNCTION("closePipe", PipeClose), + + /* usb request */ + DECLARE_NAPI_FUNCTION("initializeRequest", RequestInitialize), + DECLARE_NAPI_FUNCTION("finalizeRequest", RequestFree), + DECLARE_NAPI_FUNCTION("abortRequest", RequestAbort), + DECLARE_NAPI_FUNCTION("setClientData", RequestSetClientData), + DECLARE_NAPI_FUNCTION("getClientData", RequestGetClientData), + DECLARE_NAPI_FUNCTION("queueRequest", RequestQueue), + + DECLARE_NAPI_FUNCTION("getVersion", GetVersion) + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + + USB_HILOGD(MODULE_JS_NAPI, "return"); + + return exports; +} +EXTERN_C_END + +/* + * Module definition + */ +static napi_module g_module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = "usb", + .nm_register_func = UsbInit, + .nm_modname = "usb", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +/* + * Module registration + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&g_module); +} diff --git a/interfaces/kits/js/test/usb_unit.test.js b/interfaces/kits/js/test/usb_unit.test.js new file mode 100644 index 00000000..c179a733 --- /dev/null +++ b/interfaces/kits/js/test/usb_unit.test.js @@ -0,0 +1,495 @@ +/* + * Copyright (C) 2021 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. + */ + +import app from '@system.app' +import Context from '@ohos.napi_context' +import usb from '@ohos.usb'; + +import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' + +describe('appInfoTest', function () { + console.log("*************Usb Unit Test Begin*************"); + /* usb core functions test */ + it('get_devices_test', 0, function () { + var result = usb.getDevices(); + console.info('getDevices = ' + result); + expect(result.size() > 0).assertEqual('1'); + }) + it('connect_devices_test', 0, function () { + var device = { + busNum = 1, + devAddr = 1, + name = "", + manufacturerName = "", + productName = "", + version = "", + vendorId = 1, + productId = 1, + clazz = 1, + subclass = 1, + protocol = 1, + hasAudioPlayback = false, + hasAudioCapture = false, + hasMidi = false, + hasVideoPlayback = false, + hasVideoCapture = false, + configs: [{ + id = 1, + attributes = 1, + maxPower = 1, + name = "", + isRemoteWakeup = false, + isSelfPowered = false, + interfaces = [{ + id = 1, + protocol = 1, + clazz = 1, + subclass = 1, + alternateSetting = 1, + name = "", + endpoints = [{ + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }] + }] + }] + }; + var result = usb.connectDevice(device); + console.info('connectDevice = ' + result); + expect(Object.keys(result).length >0).assertEqual('1'); + }) + + it('has_right_test', 0, function () { + var deviceName = ""; + var result = usb.hasRight(deviceName); + console.info('hasRight = ' + result); + expect(result === true).assertEqual('1'); + }) + it('request_right_test', 0, function () { + var deviceName = ""; + var result = usb.requestRight(deviceName); + console.info('requestRight = ' + result); + expect(result === true).assertEqual('1'); + }) + + it('usb_functions_from_string_test', 0, function () { + var funcs = ""; + var result = usb.usbFunctionsFromString(funcs); + console.info('usbFunctionsFromString = ' + result); + expect(result > 0).assertEqual('1'); + }) + it('usb_functions_to_string_test', 0, function () { + var funcs = 1; + var result = usb.usbFunctionsToString(funcs); + console.info('usbFunctionsToString = ' + result); + expect(typeof result == "undefined" || result == null || result == "").assertEqual('1'); + }) + it('set_current_functions_test', 0, function () { + var funcs = 1; + var result = usb.setCurrentFunctions(funcs); + console.info('setCurrentFunctions = ' + result); + expect(result === true).assertEqual('1'); + }) + it('get_current_functions_test', 0, function () { + var result = usb.getCurrentFunctions(); + console.info('getCurrentFunctions = ' + result); + expect(typeof result == "undefined" || result == null || result == "").assertEqual('1'); + }) + it('set_hdc_enabled_test', 0, function () { + var enable = false; + var result = usb.setHdcEnabled(enable); + console.info('setHdcEnabled = ' + result); + expect(result === true).assertEqual('1'); + }) + + it('get_ports_test', 0, function () { + var result = usb.getPorts(); + console.info('getPorts = ' + result); + expect(result.size() > 0).assertEqual('1'); + }) + + /* usb port functions test */ + it('get_supported_modes_test', 0, function () { + var portId = 1; + var result = usb.getSupportedModes(portId); + console.info('getSupportedModes = ' + result); + expect(result > 0).assertEqual('1'); + }) + it('set_port_roles_test', 0, function () { + var portId = 1; + var powerRole = 1; + var dataRole = 1; + var result = usb.setPortRoles(portId, powerRole, dataRole); + console.info('setPortRoles = ' + result); + expect(result === true).assertEqual('1'); + }) + + /* usb pipe functions test */ + it('claim_interface_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var iface = { + id = 1, + protocol = 1, + clazz = 1, + subclass = 1, + alternateSetting = 1, + name = "", + endpoints = [{ + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }] + }; + var force = true; + var result = usb.claimInterface(pipe, iface, force); + console.info('claimInterface = ' + result); + expect(result === true).assertEqual('1'); + }) + it('release_interface_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var iface = { + id = 1, + protocol = 1, + clazz = 1, + subclass = 1, + alternateSetting = 1, + name = "", + endpoints = [{ + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }] + }; + var result = usb.releaseInterface(pipe, iface); + console.info('releaseInterface = ' + result); + expect(result === true).assertEqual('1'); + }) + it('get_serial_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var result = usb.getSerial(pipe); + console.info('getSerial = ' + result); + expect(typeof result == "undefined" || result == null || result == "").assertEqual('1'); + }) + it('set_configuration_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var config = { + id = 1, + attributes = 1, + maxPower = 1, + name = "", + isRemoteWakeup = false, + isSelfPowered = false, + interfaces = [{ + id = 1, + protocol = 1, + clazz = 1, + subclass = 1, + alternateSetting = 1, + name = "", + endpoints = [{ + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }] + }] + }; + var result = usb.setConfiguration(pipe, config); + console.info('setConfiguration = ' + result); + expect(result === true).assertEqual('1'); + }) + it('set_interface_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var iface = { + id = 1, + protocol = 1, + clazz = 1, + subclass = 1, + alternateSetting = 1, + name = "", + endpoints = [{ + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }] + }; + var result = usb.setInterface(pipe, iface); + console.info('setInterface = ' + result); + expect(result === true).assertEqual('1'); + }) + it('get_raw_descriptors_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var result = usb.getRawDescriptors(pipe); + console.info('getRawDescriptors = ' + result); + expect(result.length > 0).assertEqual('1'); + }) + it('get_file_descriptors_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var result = usb.getFileDescriptors(pipe); + console.info('getFileDescriptors = ' + result); + expect(result > 0).assertEqual('1'); + }) + + it('control_transfer_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var requestType = 1; + var request = 1; + var value = 1; + var index = 1; + var buffer = [1,2,3]; + var length = 1; + var timeout = 1; + var result = usb.controlTransfer(pipe, requestType, request, value, index, buffer, length,timeout); + console.info('controlTransfer = ' + result); + expect(result > 0).assertEqual('1'); + }) + it('bulk_transfer_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var endpoint = { + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }; + var buffer = [1,2,3]; + var length = 1; + var timeout = 1; + var result = usb.bulkTransfer(pipe, endpoint, buffer, length, timeout); + console.info('bulkTransfer = ' + result); + expect(result > 0).assertEqual('1'); + }) + + it('request_wait_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var result = usb.requestWait(pipe); + console.info('requestWait = ' + result); + expect(typeof result == "undefined" || result == null || result == "").assertEqual('1'); + }) + it('request_wait_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var timeout = 1; + var result = usb.requestWait(pipe, timeout); + console.info('requestWait = ' + result); + expect(typeof result == "undefined" || result == null || result == "").assertEqual('1'); + }) + it('close_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var result = usb.close(pipe, timeout); + console.info('close = ' + result); + expect(result === true).assertEqual('1'); + }) + + /* usb request functions test */ + it('initialize_test', 0, function () { + var pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }; + var endpoint = { + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }; + var result = usb.initialize(pipe, endpoint); + console.info('initialize = ' + result); + expect(typeof result == "undefined" || result == null || result == "").assertEqual('1'); + }) + it('free_test', 0, function () { + var request = { + pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }, + endpoint = { + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }, + buffer = [1,2,3], + clientData = [1,2,3] + }; + var result = usb.free(request); + console.info('free = ' + result); + expect(result === true).assertEqual('1'); + }) + it('abort_test', 0, function () { + var request = { + pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }, + endpoint = { + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }, + buffer = [1,2,3], + clientData = [1,2,3] + }; + var result = usb.abort(request); + console.info('abort = ' + result); + expect(result === true).assertEqual('1'); + }) + it('get_client_data_test', 0, function () { + var request = { + pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }, + endpoint = { + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }, + buffer = [1,2,3], + clientData = [1,2,3] + }; + var result = usb.getClientData(request); + console.info('getClientData = ' + result); + expect(result.length() > 0).assertEqual('1'); + }) + it('queue_test', 0, function () { + var request = { + pipe = { + fd = 1, + serial = "", + busNum = 1, + devAddr = 1 + }, + endpoint = { + address = 1, + attributes = 1, + interval = 1, + maxPacketSize = 1, + direction = 1, + number = 1, + type = 1 + }, + buffer = [1,2,3], + clientData = [1,2,3] + }; + var buffer = [1,2,3]; + var result = usb.queue(request, buffer); + console.info('queue = ' + result); + expect(result === true).assertEqual('1'); + }) +}) \ No newline at end of file diff --git a/ohos.build b/ohos.build new file mode 100644 index 00000000..c8dba5a9 --- /dev/null +++ b/ohos.build @@ -0,0 +1,35 @@ +{ + "subsystem": "usbmgr", + "parts": { + "usb_manager_native": { + "module_list": [ + "//base/usb/usb_manager/hdi:hdi_group", + "//base/usb/usb_manager/interfaces/innerkits:usbsrv_client", + "//base/usb/usb_manager/sa_profile:usbmgr_sa_profile", + "//base/usb/usb_manager/services:usbservice", + "//base/usb/usb_manager/interfaces/kits/js/napi:usb", + "//base/usb/usb_manager/services:usb_device_test", + "//base/usb/usb_manager/services:usb_port_test", + "//base/usb/usb_manager/services:usb_function_test" + ], + "inner_kits": [ + { + "name": "//base/usb/usb_manager/interfaces/innerkits:usbsrv_client", + "header": { + "header_files": [ + "usb_srv_client.h", + "iusb_srv.h", + "usb_info.h" + ], + "header_base": "//base/usb/usb_manager/interfaces/innerkits/native/include" + } + } + ], + "test_list": [ + "//base/usb/usb_manager/services/native/test:usb_service_unittest", + "//base/usb/usb_manager/hdi/test:usbd_client_unittest", + "//base/usb/usb_manager/test/native:usbmgr_native_test" + ] + } + } +} diff --git a/sa_profile/4201.xml b/sa_profile/4201.xml new file mode 100644 index 00000000..cbee7ccb --- /dev/null +++ b/sa_profile/4201.xml @@ -0,0 +1,24 @@ + + + + foundation + + 4201 + libusbservice.z.so + true + false + 1 + + diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn new file mode 100644 index 00000000..67e668fa --- /dev/null +++ b/sa_profile/BUILD.gn @@ -0,0 +1,19 @@ +# Copyright (c) 2021 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. + +import("//build/ohos/sa_profile/sa_profile.gni") + +ohos_sa_profile("usbmgr_sa_profile") { + sources = [ "4201.xml" ] + part_name = "usb_manager_native" +} diff --git a/services/BUILD.gn b/services/BUILD.gn new file mode 100644 index 00000000..14e609c3 --- /dev/null +++ b/services/BUILD.gn @@ -0,0 +1,176 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") + +config("usbsrv_private_config") { + include_dirs = [ "//utils/system/safwk/native/include" ] +} + +config("usbsrv_public_config") { + include_dirs = [ + "native/include", + "${usb_manager_path}/services/zidl/include", + ] +} + +ohos_shared_library("usbservice") { + sources = [ + "${usb_manager_path}/services/zidl/src/usb_srv_stub.cpp", + "native/src/usb_descriptor_parser.cpp", + "native/src/usb_device_manager.cpp", + "native/src/usb_host_manager.cpp", + "native/src/usb_permission_manager.cpp", + "native/src/usb_port_manager.cpp", + "native/src/usb_serial_reader.cpp", + "native/src/usb_server_event_handler.cpp", + "native/src/usb_service_subscriber.cpp", + "native/src/usb_service.cpp", + "native/src/usb_function_manager.cpp", + ] + + configs = [ + "${utils_path}:utils_config", + ":usbsrv_private_config", + ] + + public_configs = [ ":usbsrv_public_config" ] + + deps = [ + "${usb_manager_path}/hdi/client:usbd_client", + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "//utils/native/base:utils", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + + part_name = "usb_manager_native" +} + +ohos_executable("usb_function_test") { + sources = [ + "${usb_manager_path}/services/zidl/src/usb_srv_proxy.cpp", + "${usb_manager_path}/services/native/src/usb_function_test.cpp", + ] + + include_dirs = [ + "native/include", + "//utils/native/base/include", + "//base/usb/usb_manager/interfaces/innerkits/native/include", + "//base/usb/usb_manager/utils/native/include", + "${usb_manager_path}/services/zidl/include", + "//utils/system/safwk/native/include", + ] + + deps = [ + "//utils/native/base:utils", + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + + install_enable = true + part_name = "usb_manager_native" +} + +ohos_executable("usb_port_test") { + sources = [ + "${usb_manager_path}/services/zidl/src/usb_srv_proxy.cpp", + "${usb_manager_path}/services/native/src/usb_port_test.cpp", + ] + + include_dirs = [ + "native/include", + "//utils/native/base/include", + "//base/usb/usb_manager/interfaces/innerkits/native/include", + "//base/usb/usb_manager/utils/native/include", + "${usb_manager_path}/services/zidl/include", + "//utils/system/safwk/native/include", + ] + + deps = [ + "//utils/native/base:utils", + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + + install_enable = true + part_name = "usb_manager_native" +} + +ohos_executable("usb_device_test") { + sources = [ + "${usb_manager_path}/services/zidl/src/usb_srv_proxy.cpp", + "${usb_manager_path}/services/native/src/usb_test.cpp", + ] + + include_dirs = [ + "native/include", + "//utils/native/base/include", + "//base/usb/usb_manager/interfaces/innerkits/native/include", + "//base/usb/usb_manager/utils/native/include", + "${usb_manager_path}/services/zidl/include", + "//utils/system/safwk/native/include", + ] + + deps = [ + "//utils/native/base:utils", + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] + + install_enable = true + part_name = "usb_manager_native" +} diff --git a/services/native/include/usb_descriptor_parser.h b/services/native/include/usb_descriptor_parser.h new file mode 100644 index 00000000..b5674349 --- /dev/null +++ b/services/native/include/usb_descriptor_parser.h @@ -0,0 +1,78 @@ +#ifndef USB_DESCRIPTOR_PARSER_H +#define USB_DESCRIPTOR_PARSER_H +#include "usb_device.h" +#include "usb_interface.h" + +namespace OHOS +{ + namespace USB + { + + class UsbDescriptorParser + { + public: + UsbDescriptorParser(); + ~UsbDescriptorParser(); + + static int32_t ParseDeviceDescriptor(const uint8_t *buffer, uint32_t length, UsbDevice &dev); + static int32_t ParseConfigDescriptor(const uint8_t *buffer, uint32_t length, int32_t &cursor, USBConfig &config); + static int32_t ParseInterfaceDescriptor(const uint8_t *buffer, uint32_t length, int32_t &cursor, UsbInterface &interface); + static int32_t ParseEndpointDescriptor(const uint8_t *buffer, uint32_t length, int32_t &cursor, USBEndpoint &ep); + + private: + #if 0 + struct UsbdDeviceDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize0; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacturer; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; + } __attribute__ ((packed)); + + struct UsbdConfigDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t bMaxPower; + } __attribute__ ((packed)); + + struct UsbdInterfaceDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; + } __attribute__ ((packed)); + + struct UsbdEndpointDescriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint16_t wMaxPacketSize; + uint8_t bInterval; + uint8_t bRefresh; + uint8_t bSynchAddress; + } __attribute__ ((packed)); + #endif + }; + } +} +#endif diff --git a/services/native/include/usb_device_manager.h b/services/native/include/usb_device_manager.h new file mode 100644 index 00000000..017df4e7 --- /dev/null +++ b/services/native/include/usb_device_manager.h @@ -0,0 +1,21 @@ +#ifndef USB_DEVICE_MANARER_H +#define USB_DEVICE_MANARER_H + +#include + +namespace OHOS { +namespace USB { + + class UsbDeviceManager { + public: + bool AddFunction(std::string functions, std::string function); + bool RemoveFunction(std::string functions, std::string function); + + public: + UsbDeviceManager(); + ~UsbDeviceManager(); + }; + +} // namespace USB +} // namespace OHOS +#endif diff --git a/services/native/include/usb_function_manager.h b/services/native/include/usb_function_manager.h new file mode 100644 index 00000000..e6455222 --- /dev/null +++ b/services/native/include/usb_function_manager.h @@ -0,0 +1,40 @@ +#ifndef USB_FUNCTION_MANAGER_H +#define USB_FUNCTION_MANAGER_H + +#include +#include +#include + +namespace OHOS { +namespace USB { + +class UsbFunctionManager { +public: + + static bool AreSettableFunctions(int32_t funcs); + + static int32_t FromStringFunctions(std::string funcs); + static std::string ToStringFunctions(int32_t func); + static void updateFunctions(int32_t func); + static int32_t getCurrentFunctions(); + + static const std::string FUNCTION_NAME_NONE; + static const std::string FUNCTION_NAME_HDC; + static const std::string FUNCTION_NAME_ACM; + static const std::string FUNCTION_NAME_ECM; + + static const int32_t FUNCTION_NONE; + static const int32_t FUNCTION_ACM; + static const int32_t FUNCTION_ECM; + static const int32_t FUNCTION_HDC; + +private: + static const int32_t FUNCTION_SETTABLE; + static const std::map FUNCTION_MAPPING_N2C; + static int currentFunctions; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USB_FUNCTION_MANAGER_H diff --git a/services/native/include/usb_host_manager.h b/services/native/include/usb_host_manager.h new file mode 100644 index 00000000..f3df49fc --- /dev/null +++ b/services/native/include/usb_host_manager.h @@ -0,0 +1,57 @@ +#ifndef USB_HOST_MANAGER_H +#define USB_HOST_MANAGER_H + +#include + +#include "system_ability.h" +#include "usb_permission_manager.h" +#include "usb_device.h" +#include +#include +#include + +namespace OHOS +{ + namespace USB + { + typedef std::map MAP_STR_DEVICE; + + class UsbHostManager + { + private: /* data */ + std::vector hostDenyList_; + MAP_STR_DEVICE devices_; + bool DEBUG_ = false; + UsbPermissionManager *permissionManager_; + SystemAbility *systemAbility_; + + public: + bool usbDeviceAdded(std::string deviceAddress, int deviceClass, int deviceSubclass, + unsigned char * descriptors, + unsigned int descriptorsSize);// override; + //bool AddDevice(uint8_t busNum, uint8_t devNum); + bool DelDevice(uint8_t busNum, uint8_t devNum); + bool AddDevice(UsbDevice *dev); + + public: + UsbHostManager(SystemAbility *systemAbility, + UsbPermissionManager *permissionManager); + ~UsbHostManager(); + void getDevices(MAP_STR_DEVICE &devices); + + /** + * Opens the specified USB device + */ + void openDevice(unsigned char *parcel, unsigned int parcelSize, std::string deviceAddress, + std::string packageName, int pid, int uid); + bool isDenyListed(std::string deviceAddress); + + //void logUsbDevice(UsbDescriptorParser descriptorParser) {} + //bool checkUsbInterfacesDenyListed(UsbDescriptorParser parser) {}; + //void nativeOpenDevice(ParcelFileDescriptor parcel, std::string deviceAddress); + }; + + } +} + +#endif \ No newline at end of file diff --git a/services/native/include/usb_permission_manager.h b/services/native/include/usb_permission_manager.h new file mode 100644 index 00000000..45a9d892 --- /dev/null +++ b/services/native/include/usb_permission_manager.h @@ -0,0 +1,38 @@ +#ifndef USB_PERMISSION_MANAGER_H +#define USB_PERMISSION_MANAGER_H + +#include +#include +#include +#include +#include +#include "usb_common.h" + +namespace OHOS +{ + namespace USB + { + + class UsbPermissionManager + { + public: + + typedef std::vector UidList; + typedef std::map PermissionMap; + PermissionMap permissionMap; + + void Init(); + int32_t HasPermission(std::string deviceName, int uid); + int32_t RequestPermission(std::string deviceName, int uid); + void AddDevicePermission(std::string deviceName, int uid); + void RemoveDevicePermission(std::string deviceName); + + private: + + }; + + } + +} + +#endif diff --git a/services/native/include/usb_port_manager.h b/services/native/include/usb_port_manager.h new file mode 100644 index 00000000..3df9b0a0 --- /dev/null +++ b/services/native/include/usb_port_manager.h @@ -0,0 +1,40 @@ +#ifndef USB_PORT_MANAGER_H +#define USB_PORT_MANAGER_H + +#include +#include +#include +#include +#include "usb_common.h" +#include "usb_port.h" + +namespace OHOS +{ + namespace USB + { + class UsbPortManager + { + public: + UsbPortManager(); + typedef std::map PortMap; + PortMap portMap; + + void Init(); + void Init(int32_t test); + int32_t GetPorts(std::vector &result); + int32_t GetSupportedModes(int32_t portId, int32_t &result); + void SetPortRoles(int32_t portId, int32_t powerRole, int32_t dataRole); + int32_t QueryPort(); + void UpdatePort(int32_t portId, int32_t powerRole, int32_t dataRole); + void AddPort(UsbPort *port); + void RemovePort(int32_t portId); + + private: + + }; + + } + +} + +#endif diff --git a/services/native/include/usb_serial_reader.h b/services/native/include/usb_serial_reader.h new file mode 100644 index 00000000..bb1f27e1 --- /dev/null +++ b/services/native/include/usb_serial_reader.h @@ -0,0 +1,30 @@ +#ifndef USB_SERIAL_READER_H +#define USB_SERIAL_READER_H + +#include "usb_permission_manager.h" +#include "system_ability.h" +#include + +namespace OHOS +{ + namespace USB + { + + class UsbSerialReader + { + public: + UsbSerialReader(SystemAbility *systemAbility, + UsbPermissionManager *permissionManager, std::string serialNumber); + ~UsbSerialReader(); + + private: + SystemAbility *systemAbility_; + UsbPermissionManager *permissionManager_; + std::string serialNumber_; + }; + + } + +} + +#endif \ No newline at end of file diff --git a/services/native/include/usb_server_event_handler.h b/services/native/include/usb_server_event_handler.h new file mode 100644 index 00000000..9ffa5c4d --- /dev/null +++ b/services/native/include/usb_server_event_handler.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBMGR_USB_SERVER_EVENT_HANDLER_H +#define USBMGR_USB_SERVER_EVENT_HANDLER_H + +#include "refbase.h" +#include "event_handler.h" + +namespace OHOS { +namespace USB { + +class UsbService; + +class UsbServerEventHandler : public AppExecFwk::EventHandler { +public: + UsbServerEventHandler(const std::shared_ptr &runner, + const wptr &service); + ~UsbServerEventHandler() = default; + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; + +private: + wptr service_; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USBMGR_USB_SERVER_EVENT_HANDLER_H diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h new file mode 100644 index 00000000..7cc40d42 --- /dev/null +++ b/services/native/include/usb_service.h @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBMGR_USB_SERVICE_H +#define USBMGR_USB_SERVICE_H + +#include "sp_singleton.h" +#include "system_ability.h" +#include "iremote_object.h" +#include "iusb_srv.h" +#include "usbd_api.h" +#include "usbd_client.h" +#include "usbd_subscriber.h" +#include "usb_server_stub.h" +#include "usb_server_event_handler.h" +#include "usb_service_subscriber.h" +#include "usb_host_manager.h" +#include "usb_device_manager.h" +#include "usb_permission_manager.h" +#include "usb_port_manager.h" +#include "usb_function_manager.h" + +#include +#include + +namespace OHOS { +namespace USB { + +class UsbService : public SystemAbility, public UsbServerStub { + +DECLARE_SYSTEM_ABILITY(UsbService) +DECLARE_DELAYED_SP_SINGLETON(UsbService); + +public: + virtual void OnStart() override; + virtual void OnStop() override; + virtual int32_t OnFoundDevice(FoundDevice &info) override; + + bool IsServiceReady() const { return ready_; } + std::shared_ptr GetHandler() const { return handler_; } + + int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override; + int32_t HasRight(std::string deviceName) override; + int32_t RequestRight(std::string deviceName) override; + int32_t GetDevice(uint8_t busNum, uint8_t devAddr, UsbDevice &devInfo) override; + int32_t GetDevices(std::vector &deviceList) override; + int32_t GetCurrentFunctions() override; + bool SetCurrentFunctions(int32_t funcs) override; + int32_t UsbFunctionsFromString(std::string funcs) override; + std::string UsbFunctionsToString(int32_t funcs) override; + int32_t GetPorts(std::vector &result) override; + int32_t GetSupportedModes(int32_t portId,int32_t &result) override; + int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole) override; + void usbDeviceAdded(std::string deviceAddress, int deviceClass, int deviceSubclass, + unsigned char * descriptors, + unsigned int descriptorsSize); + + int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override; + int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override; + int32_t BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout) override; + int32_t BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpoint, const uint8_t *buffer, uint32_t length, int32_t timeout) override; + int32_t ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout) override; + int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override; + int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) override; + int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) override; + int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *desc, uint32_t &length) override; + int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointId, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) override; + int32_t RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, void *clientData, uint32_t &clientLength, uint8_t *buffer, uint32_t &length) override; + int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) override; + int32_t Close(uint8_t busNum, uint8_t devAddr) override; + bool AddDevice(uint8_t busNum, uint8_t devAddr); + bool DelDevice(uint8_t busNum, uint8_t devAddr); + void UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole); + int32_t GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice *dev); + +private: + bool Init(); + bool InitUsbd(); + bool IsCommonEventServiceAbilityExist(); + + bool ready_ {false}; + int32_t commEventRetryTimes_ {0}; + std::mutex mutex_; + UsbHostManager *usbHostManger_ {nullptr}; + UsbDeviceManager *usbDeviceManager_ {nullptr}; + UsbPermissionManager *usbPermissionManager {nullptr}; + UsbPortManager *usbPortManager {nullptr}; + UsbFunctionManager *usbFunctionManager {nullptr}; + std::shared_ptr eventRunner_; + std::shared_ptr handler_; + sptr usbdSubscriber_; + std::map portMap; +}; + +} // namespace USB +} // namespace OHOS + +#endif // USBMGR_USB_SERVICE_H diff --git a/services/native/include/usb_service_subscriber.h b/services/native/include/usb_service_subscriber.h new file mode 100644 index 00000000..f0bfc456 --- /dev/null +++ b/services/native/include/usb_service_subscriber.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBMGR_USB_SERVICE_SUBSCRIBER_H +#define USBMGR_USB_SERVICE_SUBSCRIBER_H + +#include "usbd_subscriber.h" + +namespace OHOS { +namespace USB { + +class UsbServiceSubscriber : public UsbdSubscriber { +public: + UsbServiceSubscriber(); + ~UsbServiceSubscriber() = default; + int32_t Update(const UsbInfo &info) override; + int32_t DeviceEvent(const UsbInfo &info) override; + int32_t PortChangedEvent(int32_t portId, int32_t powerRole, int32_t dataRole) override; +private: + //static bool HandleUsbPortChangedEvent(const UsbInfo &info); +}; + +} // namespace USB +} // namespace OHOS + +#endif // USBMGR_USB_SERVICE_SUBSCRIBER_H diff --git a/services/native/src/usb_descriptor_parser.cpp b/services/native/src/usb_descriptor_parser.cpp new file mode 100644 index 00000000..ab3f66b4 --- /dev/null +++ b/services/native/src/usb_descriptor_parser.cpp @@ -0,0 +1,285 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_descriptor_parser.h" +#include +#include "hilog_wrapper.h" +#include "message_parcel.h" +#include "securec.h" +#include "usb_config.h" +#include "usb_interface.h" +#include "usb_endpoint.h" +#include "usbd_api.h" + +namespace OHOS { +namespace USB { +enum DescriptorType { + DESCRIPTOR_TYPE_DEVICE = 1, + DESCRIPTOR_TYPE_CONFIG = 2, + DESCRIPTOR_TYPE_INTERFACE = 4, + DESCRIPTOR_TYPE_ENDPOINT = 5 +}; + +UsbDescriptorParser::UsbDescriptorParser() {} + +UsbDescriptorParser::~UsbDescriptorParser() {} + +int32_t UsbDescriptorParser::ParseDeviceDescriptor(const uint8_t *buffer, uint32_t length, UsbDevice &dev) +{ + int32_t cursor = 0; + if (buffer == nullptr || length == 0) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang buffer is null", __func__); + return ERR_NO_INIT; + } + + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang parse begin buffer=%{public}p, length=%{public}d, cursor=%{public}d", __func__, buffer, length, cursor); + uint32_t deviceDescriptorSize = sizeof(UsbdDeviceDescriptor); + if(length < deviceDescriptorSize) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang buffer size error", __func__); + return ERR_INVALID_VALUE; + } + + UsbdDeviceDescriptor deviceDescriptor = *(UsbdDeviceDescriptor*)buffer; + cursor += deviceDescriptorSize; + if(deviceDescriptor.bLength != deviceDescriptorSize) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang UsbdDeviceDescriptor size error", __func__); + return ERR_INVALID_VALUE; + } + + dev.SetVendorId(deviceDescriptor.idVendor); + dev.SetProductId(deviceDescriptor.idProduct); + dev.SetClass(deviceDescriptor.bDeviceClass); + dev.SetSubclass(deviceDescriptor.bDeviceSubClass); + dev.SetDescConfigCount(deviceDescriptor.bNumConfigurations); + + dev.SetbMaxPacketSize0(deviceDescriptor.bMaxPacketSize0); + dev.SetbcdDevice(deviceDescriptor.bcdDevice); + dev.SetbcdUSB(deviceDescriptor.bcdUSB); + dev.SetiManufacturer(deviceDescriptor.iManufacturer); + dev.SetiProduct(deviceDescriptor.iProduct); + dev.SetiSerialNumber(deviceDescriptor.iSerialNumber); + + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang bLength=%{public}d, \ + bDescriptorType=%{public}d, \ + bcdUSB=%{public}d, \ + bDeviceClass=%{public}d, \ + bDeviceSubClass=%{public}d, \ + bDeviceProtocol=%{public}d, \ + bMaxPacketSize0=%{public}d, \ + idVendor=%{public}d, \ + idProduct=%{public}d, \ + bcdDevice=%{public}d, \ + iManufacturer=%{public}d, \ + iProduct=%{public}d, \ + iSerialNumber=%{public}d, \ + bNumConfigurations=%{public}d", + __func__, deviceDescriptor.bLength, + deviceDescriptor.bDescriptorType, + deviceDescriptor.bcdUSB, + deviceDescriptor.bDeviceClass, + deviceDescriptor.bDeviceSubClass, + deviceDescriptor.bDeviceProtocol, + deviceDescriptor.bMaxPacketSize0, + deviceDescriptor.idVendor, + deviceDescriptor.idProduct, + deviceDescriptor.bcdDevice, + deviceDescriptor.iManufacturer, + deviceDescriptor.iProduct, + deviceDescriptor.iSerialNumber, + deviceDescriptor.bNumConfigurations + ); + + return ERR_OK; +} + +int32_t UsbDescriptorParser::ParseConfigDescriptor(const uint8_t *buffer, uint32_t length, int32_t &cursor, USBConfig &config) +{ + if (buffer == nullptr || length == 0) { + return ERR_INVALID_VALUE; + } + + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang parse begin buffer=%{public}p, length=%{public}d, cursor=%{public}d", __func__, buffer, length, cursor); + uint32_t configDescriptorSize = sizeof(UsbdConfigDescriptor); + if(length < configDescriptorSize) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang buffer size error", __func__); + return ERR_INVALID_VALUE; + } + + UsbdConfigDescriptor configDescriptor = *(UsbdConfigDescriptor*)buffer; + cursor += configDescriptorSize; + if(configDescriptor.bLength != configDescriptorSize) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang UsbdDeviceDescriptor size error", __func__); + return ERR_INVALID_VALUE; + } + + config.SetId(configDescriptor.bConfigurationValue); + config.SetAttribute(configDescriptor.bmAttributes); + config.SetMaxPower(configDescriptor.bMaxPower); + config.SetiConfiguration(configDescriptor.iConfiguration); + + std::vector interfaces; + for(int i = 0; i < configDescriptor.bNumInterfaces; ++i) { + int32_t interfaceCursor = 0; + UsbInterface interface; + ParseInterfaceDescriptor(buffer + cursor + interfaceCursor, length - cursor - interfaceCursor, interfaceCursor, interface); + interfaces.push_back(interface); + cursor += interfaceCursor; + } + config.SetInterfaces(interfaces); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang bLength=%{public}d, \ + bDescriptorType=%{public}d, \ + wTotalLength=%{public}d, \ + bNumInterfaces=%{public}d, \ + bConfigurationValue=%{public}d, \ + iConfiguration=%{public}d, \ + bmAttributes=%{public}d, \ + bMaxPower=%{public}d", + __func__, configDescriptor.bLength, + configDescriptor.bDescriptorType, + configDescriptor.wTotalLength, + configDescriptor.bNumInterfaces, + configDescriptor.bConfigurationValue, + configDescriptor.iConfiguration, + configDescriptor.bmAttributes, + configDescriptor.bMaxPower + ); + return ERR_OK; +} + +int32_t UsbDescriptorParser::ParseInterfaceDescriptor(const uint8_t *buffer, uint32_t length, int32_t &cursor, UsbInterface &interface) +{ + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang parse begin buffer=%{public}p, length=%{public}d, cursor=%{public}d", __func__, buffer, length, cursor); + if (buffer == nullptr || length == 0) { + return ERR_INVALID_VALUE; + } + + uint32_t descriptorHeaderSize = sizeof(UsbdDescriptorHeader); + while((uint32_t)cursor < length) { + if(descriptorHeaderSize >= length) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: descriptor size error", __func__); + return ERR_INVALID_VALUE; + } + UsbdDescriptorHeader descriptorHeader = *(UsbdDescriptorHeader*)(buffer + cursor); + if(descriptorHeader.bLength > length) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: descriptor size error", __func__); + return ERR_INVALID_VALUE; + } + if(descriptorHeader.bDescriptorType == DESCRIPTOR_TYPE_INTERFACE) { + break; + } + cursor += descriptorHeader.bLength; + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: error type = %{public}d, length=%{public}d", __func__, descriptorHeader.bDescriptorType, descriptorHeader.bLength); + } + + UsbdInterfaceDescriptor interfaceDescriptor = *(UsbdInterfaceDescriptor*)(buffer + cursor); + if(interfaceDescriptor.bLength != sizeof(UsbdInterfaceDescriptor)) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang UsbdInterfaceDescriptor size error", __func__); + return ERR_INVALID_VALUE; + } + cursor += interfaceDescriptor.bLength; + + interface.SetId(interfaceDescriptor.bInterfaceNumber); + interface.SetProtocol(interfaceDescriptor.bInterfaceProtocol); + interface.SetAlternateSetting(interfaceDescriptor.bAlternateSetting); + interface.SetClass(interfaceDescriptor.bInterfaceClass); + interface.SetSubClass(interfaceDescriptor.bInterfaceSubClass); + interface.SetiInterface(interfaceDescriptor.iInterface); + + std::vector eps; + for (int j = 0; j < interfaceDescriptor.bNumEndpoints; ++j) { + int32_t epCursor = 0; + USBEndpoint ep; + ParseEndpointDescriptor(buffer + cursor + epCursor, length - cursor - epCursor, epCursor, ep); + ep.SetInterfaceId(interfaceDescriptor.bInterfaceNumber); + eps.push_back(ep); + cursor += epCursor; + } + interface.SetEndpoints(eps); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang bLength=%{public}d, \ + bDescriptorType=%{public}d, \ + bInterfaceNumber=%{public}d, \ + bAlternateSetting=%{public}d, \ + bNumEndpoints=%{public}d, \ + bInterfaceClass=%{public}d, \ + bInterfaceSubClass=%{public}d, \ + bInterfaceProtocol=%{public}d, \ + iInterface=%{public}d", + __func__, interfaceDescriptor.bLength, + interfaceDescriptor.bDescriptorType, + interfaceDescriptor.bInterfaceNumber, + interfaceDescriptor.bAlternateSetting, + interfaceDescriptor.bNumEndpoints, + interfaceDescriptor.bInterfaceClass, + interfaceDescriptor.bInterfaceSubClass, + interfaceDescriptor.bInterfaceProtocol, + interfaceDescriptor.iInterface + ); + + + return ERR_OK; +} +int32_t UsbDescriptorParser::ParseEndpointDescriptor(const uint8_t *buffer, uint32_t length, int32_t &cursor, USBEndpoint &ep) +{ + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang parse begin buffer=%{public}p, length=%{public}d, cursor=%{public}d", __func__, buffer, length, cursor); + if (buffer == nullptr || length == 0) { + return ERR_INVALID_VALUE; + } + + uint32_t descriptorHeaderSize = sizeof(UsbdDescriptorHeader); + while((uint32_t)cursor < length) { + if(descriptorHeaderSize >= length) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: descriptor size error", __func__); + return ERR_INVALID_VALUE; + } + UsbdDescriptorHeader descriptorHeader = *(UsbdDescriptorHeader*)(buffer + cursor); + if(descriptorHeader.bLength > length) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: descriptor size error", __func__); + return ERR_INVALID_VALUE; + } + if(descriptorHeader.bDescriptorType == DESCRIPTOR_TYPE_ENDPOINT) { + break; + } + cursor += descriptorHeader.bLength; + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: error type = %{public}d, length=%{public}d", __func__, descriptorHeader.bDescriptorType, descriptorHeader.bLength); + } + + UsbdEndpointDescriptor endpointDescriptor = *(UsbdEndpointDescriptor*)(buffer + cursor); + if(endpointDescriptor.bLength != sizeof(UsbdEndpointDescriptor)) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang Endpoint descriptor size error, length=%{public}d", __func__, endpointDescriptor.bLength); + return ERR_INVALID_VALUE; + } + cursor += endpointDescriptor.bLength; + + ep.SetAddr(endpointDescriptor.bEndpointAddress); + ep.SetAttr(endpointDescriptor.bmAttributes); + ep.SetInterval(endpointDescriptor.bInterval); + ep.SetMaxPacketSize(endpointDescriptor.wMaxPacketSize); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: fumang bLength=%{public}d, \ + bDescriptorType=%{public}d, \ + bEndpointAddress=%{public}d, \ + bmAttributes=%{public}d, \ + wMaxPacketSize=%{public}d, \ + bInterval=%{public}d", + __func__, endpointDescriptor.bLength, + endpointDescriptor.bDescriptorType, + endpointDescriptor.bEndpointAddress, + endpointDescriptor.bmAttributes, + endpointDescriptor.wMaxPacketSize, + endpointDescriptor.bInterval + ); + return ERR_OK; +} +} // namespace USB +} // namespace OHOS \ No newline at end of file diff --git a/services/native/src/usb_device_manager.cpp b/services/native/src/usb_device_manager.cpp new file mode 100644 index 00000000..4aee3f50 --- /dev/null +++ b/services/native/src/usb_device_manager.cpp @@ -0,0 +1,29 @@ +#include "usb_device_manager.h" + +namespace OHOS +{ + namespace USB + { + + UsbDeviceManager::UsbDeviceManager() + { + } + + UsbDeviceManager::~UsbDeviceManager() + { + } + + bool UsbDeviceManager::AddFunction(std::string functions, std::string function) + { + return false; + } + + bool UsbDeviceManager::RemoveFunction(std::string functions, std::string function) + { + return false; + } + + } +} + + diff --git a/services/native/src/usb_function_manager.cpp b/services/native/src/usb_function_manager.cpp new file mode 100644 index 00000000..d506f1fb --- /dev/null +++ b/services/native/src/usb_function_manager.cpp @@ -0,0 +1,76 @@ +#include "usb_function_manager.h" +#include +#include + +namespace OHOS { +namespace USB { + +const std::string UsbFunctionManager::FUNCTION_NAME_NONE = "none"; +const std::string UsbFunctionManager::FUNCTION_NAME_ACM = "acm"; +const std::string UsbFunctionManager::FUNCTION_NAME_ECM = "ecm"; +const std::string UsbFunctionManager::FUNCTION_NAME_HDC = "hdc"; + +const int32_t UsbFunctionManager::FUNCTION_NONE = 0; +const int32_t UsbFunctionManager::FUNCTION_ACM = 1; +const int32_t UsbFunctionManager::FUNCTION_ECM = 2; +const int32_t UsbFunctionManager::FUNCTION_HDC = 4; + +int32_t UsbFunctionManager::currentFunctions = 0; + +const int32_t UsbFunctionManager::FUNCTION_SETTABLE = FUNCTION_HDC | FUNCTION_ACM | FUNCTION_ECM; + +const std::map UsbFunctionManager::FUNCTION_MAPPING_N2C = { + {FUNCTION_NAME_NONE, FUNCTION_NONE}, + {FUNCTION_NAME_HDC, FUNCTION_HDC}, + {FUNCTION_NAME_ACM, FUNCTION_ACM}, + {FUNCTION_NAME_ECM, FUNCTION_ECM}, +}; + +bool UsbFunctionManager::AreSettableFunctions(int32_t funcs) { + return funcs == FUNCTION_NONE || ((~FUNCTION_SETTABLE & funcs) == 0); +} + +int32_t UsbFunctionManager::FromStringFunctions(std::string funcs) { + if (funcs.empty() || funcs.compare(FUNCTION_NAME_NONE) == 0) { + return FUNCTION_NONE; + } + + std::regex re(","); + std::vector vec(std::sregex_token_iterator(funcs.begin(), funcs.end(), re, -1), + std::sregex_token_iterator()); + int64_t ret = 0; + for (auto &&item : vec) { + auto it = FUNCTION_MAPPING_N2C.find(item); + if (it != FUNCTION_MAPPING_N2C.end()) { + ret |= it->second; + } + else { + std::string msg = "Invalid argument of usb function" + funcs; + std::__throw_invalid_argument(msg.c_str()); + } + } + return ret; +} + +std::string UsbFunctionManager::ToStringFunctions(int32_t func) { + std::stringstream stream; + if ((func & FUNCTION_HDC) != 0) { + stream << FUNCTION_NAME_HDC; + } + if ((func & FUNCTION_ACM) != 0) { + stream << ","; + stream << FUNCTION_NAME_ACM; + } + if ((func & FUNCTION_ECM) != 0) { + stream << ","; + stream << FUNCTION_NAME_ECM; + } + return stream.str(); +} + +void UsbFunctionManager::updateFunctions(int32_t func) { currentFunctions = func; } + +int32_t UsbFunctionManager::getCurrentFunctions() { return currentFunctions; } + +} // namespace USB +} // namespace OHOS diff --git a/services/native/src/usb_function_test.cpp b/services/native/src/usb_function_test.cpp new file mode 100644 index 00000000..385f9fab --- /dev/null +++ b/services/native/src/usb_function_test.cpp @@ -0,0 +1,58 @@ +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "iusb_srv.h" +#include "string_ex.h" +#include "system_ability_definition.h" +#include "usb_common.h" +#include "usb_device.h" +#include "usb_server_proxy.h" +#include "usb_srv_client.h" +#include +#include +#include + +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +int main(int argc, char **argv) { + std::cout << "argv[1] = " < proxy_; + + std::mutex mutex_; + std::lock_guard lock(mutex_); + OHOS::sptr sm = + OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + std::cout << "GetSystemAbilityManager failed" << std::endl; + return -1; + } + std::cout << "GetSystemAbilityManager successed." << std::endl; + OHOS::sptr remoteObject_ = sm->CheckSystemAbility(4201); + if (remoteObject_ == nullptr) { + std::cout << "CheckSystemAbility failed." << std::endl; + return -2; + } + + if (!remoteObject_->IsProxyObject()) { + std::cout << "IsProxyObject failed." << std::endl; + return -4; + } + + std::cout << "CheckSystemAbility successed." << std::endl; + proxy_ = OHOS::iface_cast(remoteObject_); + if (proxy_ == nullptr) { + std::cout << "proxy failed." << std::endl; + return -3; + } + std::cout << "usb_function_test : << SetCurrentFunctions >> test begin -> " << std::endl; + bool ret = g_usbClient.SetCurrentFunctions(func); + std::cout << "usb_function_test : << SetCurrentFunctions >> ret -> " << ret <> test begin -> " << std::endl; + int funcs = g_usbClient.GetCurrentFunctions(); + std::cout << "usb_function_test : << GetCurrentFunctions >> ret -> " << funcs <SetBusNum(busNum); + dev->SetDevAddr(devNum); + std::string name = std::to_string(busNum)+std::to_string(devNum); + MAP_STR_DEVICE::iterator iter = devices_.find(name); + if(iter != devices_.end()) + { + USB_HILOGF(MODULE_SERVICE, "%s: device:%s already exist", __func__, name.c_str()); + UsbDevice *devOld = iter->second; + devices_.erase(iter); + delete(devOld); + } + devices_.insert(std::pair(name,dev)); + return true; + } + */ + bool UsbHostManager::DelDevice(uint8_t busNum, uint8_t devNum) + { + std::string name = std::to_string(busNum) + "-" + std::to_string(devNum); + MAP_STR_DEVICE::iterator iter = devices_.find(name); + if(iter == devices_.end()) + { + USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d not exist", + __func__, __LINE__, name.c_str(), busNum, devNum); + return false; + } + USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d erase ", + __func__, __LINE__, name.c_str(), busNum, devNum); + devices_.erase(iter); + return true; + } + bool UsbHostManager::AddDevice(UsbDevice *dev) + { + if(dev == NULL){ + USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device is NULL", __func__, __LINE__); + return false; + } + uint8_t busNum = dev->GetBusNum(); + uint8_t devNum = dev->GetDevAddr(); + std::string name = std::to_string(busNum) + "-" + std::to_string(devNum); + MAP_STR_DEVICE::iterator iter = devices_.find(name); + if(iter != devices_.end()) + { + USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d already exist", + __func__, __LINE__, name.c_str(), busNum, devNum); + UsbDevice *devOld = iter->second; + devices_.erase(iter); + delete(devOld); + } + USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d insert", + __func__, __LINE__, name.c_str(), busNum, devNum); + devices_.insert(std::pair(name,dev)); + return true; + } + + } + +} diff --git a/services/native/src/usb_permission_manager.cpp b/services/native/src/usb_permission_manager.cpp new file mode 100644 index 00000000..3019c5d6 --- /dev/null +++ b/services/native/src/usb_permission_manager.cpp @@ -0,0 +1,65 @@ +#include "usb_permission_manager.h" + +namespace OHOS { +namespace USB { +void UsbPermissionManager::Init() {} + +int32_t UsbPermissionManager::HasPermission(std::string deviceName, int uid) { + auto itMap = permissionMap.find(deviceName); + if (itMap == permissionMap.end()) { + USB_HILOGE(MODULE_USB_SERVICE, "hasPermission empty false 1"); + return -1; + } else { + UidList uidList = itMap->second; + auto itVevtor = std::find(uidList.begin(), uidList.end(), uid); + if (itVevtor == uidList.end()) { + USB_HILOGE(MODULE_USB_SERVICE, "hasPermission uid false 2"); + return -1; + } + } + USB_HILOGI(MODULE_USB_SERVICE, "Request Permission Success 3"); + return 0; +} + +int32_t UsbPermissionManager::RequestPermission(std::string deviceName, int uid) { + if (HasPermission(deviceName, uid) == 0) { + USB_HILOGE(MODULE_USB_SERVICE, "requestPermission has permission False 1"); + return -1; + } + AddDevicePermission(deviceName, uid); + if (HasPermission(deviceName, uid) == 0) { + USB_HILOGI(MODULE_USB_SERVICE, "requestPermission Success 2"); + return 0; + } + USB_HILOGE(MODULE_USB_SERVICE, "requestPermission False 3"); + return -1; +} + +void UsbPermissionManager::AddDevicePermission(std::string deviceName, int uid) { + auto itMap = permissionMap.find(deviceName); + if (itMap != permissionMap.end()) { + auto v = itMap->second; + auto itVevtor = std::find(v.begin(), v.end(), uid); + if (itVevtor != v.end()) { + USB_HILOGE(MODULE_USB_SERVICE, "addDevicePermission false 1"); + return; + } + itMap->second.push_back(uid); + USB_HILOGI(MODULE_USB_SERVICE, "addDevicePermission success 2"); + } + UidList uidList; + uidList.push_back(uid); + permissionMap.insert(PermissionMap::value_type(deviceName, uidList)); + USB_HILOGI(MODULE_USB_SERVICE, "addDevicePermission success 3"); +} + +void UsbPermissionManager::RemoveDevicePermission(std::string deviceName) { + auto it = permissionMap.find("deviceName"); + if (it != permissionMap.end()) { + permissionMap.erase(it); + USB_HILOGI(MODULE_USB_SERVICE, "removeDevicePermission success 1"); + } +} + +} // namespace USB +} // namespace OHOS \ No newline at end of file diff --git a/services/native/src/usb_port_manager.cpp b/services/native/src/usb_port_manager.cpp new file mode 100644 index 00000000..396ebf9f --- /dev/null +++ b/services/native/src/usb_port_manager.cpp @@ -0,0 +1,118 @@ +#include "usb_port_manager.h" +#include "usbd_client.h" + +namespace OHOS { +namespace USB { + +UsbPortManager::UsbPortManager(){ + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::Init start"); + //Init(); +} + +void UsbPortManager::Init() { + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::QueryPort start"); + int ret = QueryPort(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::QueryPort end ret:%{public}d",ret); + if (ret) { + UsbPortStatus *usbPortStatus1 = new UsbPortStatus; + usbPortStatus1->currentMode = 1; + usbPortStatus1->currentDataRole = 1; + usbPortStatus1->currentPowerRole = 1; + UsbPort *usbPort1 = new UsbPort; + usbPort1->id = 1; + usbPort1->supportedModes = 2; + usbPort1->usbPortStatus = usbPortStatus1; + AddPort(usbPort1); + } +} + +int32_t UsbPortManager::GetPorts(std::vector &result) { + + if (portMap.size() > 0) { + for (auto it = portMap.begin(); it != portMap.end(); ++it) { + result.push_back(it->second); + } + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::GetPorts success"); + return ERR_OK; + } + USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::GetPorts false"); + return ERR_INVALID_VALUE; +} + +int32_t UsbPortManager::GetSupportedModes(int32_t portId, int32_t &result) { + auto it = portMap.find(portId); + if (it != portMap.end()) { + result = it->second->supportedModes; + return ERR_OK; + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::GetSupportedModes success"); + } + USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::GetSupportedModes false"); + return ERR_INVALID_VALUE; +} + +int32_t UsbPortManager::QueryPort() { + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::queryPorts run"); + int32_t portId = 0; + int32_t powerRole = 0; + int32_t dataRole = 0; + int32_t mode = 0; + int ret = UsbdClient::QueryPort(portId, powerRole, dataRole, mode); + USB_HILOGE(MODULE_USB_SERVICE, "portId:%{public}d powerRole:%{public}d dataRole:%{public}d mode:%{public}d ", portId, powerRole, dataRole, mode); + if (ret) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbdClient::queryPorts false"); + return ret; + } + UsbPortStatus *usbPortStatus = new UsbPortStatus; + UsbPort *usbPort = new UsbPort; + usbPortStatus->currentMode = mode; + usbPortStatus->currentDataRole = dataRole; + usbPortStatus->currentPowerRole = powerRole; + usbPort->id = portId; + usbPort->supportedModes = 6; + usbPort->usbPortStatus = usbPortStatus; + AddPort(usbPort); + return ret; +} + +void UsbPortManager::UpdatePort(int32_t portId, int32_t powerRole, int32_t dataRole) { + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::updatePort run"); + auto it = portMap.find(portId); + if (it != portMap.end()) { + if (it->second->id == portId) { + it->second->usbPortStatus->currentPowerRole = powerRole; + it->second->usbPortStatus->currentDataRole = dataRole; + USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::updatePort seccess"); + return; + } + } + USB_HILOGE(MODULE_USB_SERVICE, "updatePort false"); +} + +void UsbPortManager::AddPort(UsbPort *port) { + USB_HILOGI(MODULE_USB_SERVICE, "addPort run"); + auto it = portMap.find(port->id); + if (it == portMap.end()) { + portMap.insert(PortMap::value_type(port->id, port)); + USB_HILOGI(MODULE_USB_SERVICE, "addPort seccess"); + return; + }else{ + delete port; + port = nullptr; + } + USB_HILOGE(MODULE_USB_SERVICE, "addPort false"); +} + +void UsbPortManager::RemovePort(int32_t portId) { + USB_HILOGI(MODULE_USB_SERVICE, "removePort run"); + auto it = portMap.find(portId); + if (it != portMap.end()) { + portMap.erase(it); + delete it->second; + USB_HILOGI(MODULE_USB_SERVICE, "removePort seccess"); + return; + } + USB_HILOGE(MODULE_USB_SERVICE, "removePort false"); +} + +} // namespace USB +} // namespace OHOS diff --git a/services/native/src/usb_port_test.cpp b/services/native/src/usb_port_test.cpp new file mode 100644 index 00000000..d2ac5c99 --- /dev/null +++ b/services/native/src/usb_port_test.cpp @@ -0,0 +1,109 @@ +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "iusb_srv.h" +#include "string_ex.h" +#include "system_ability_definition.h" +#include "usb_common.h" +#include "usb_device.h" +#include "usb_server_proxy.h" +#include "usb_srv_client.h" +#include +#include +#include + +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +int main(int argc, char **argv) { + std::cout << "argv[1] = " << argv[1] << std::endl; + int func = atoi(argv[1]); + static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance(); + OHOS::sptr proxy_; + + std::mutex mutex_; + std::lock_guard lock(mutex_); + OHOS::sptr sm = + OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + std::cout << "GetSystemAbilityManager failed" << std::endl; + return -1; + } + std::cout << "GetSystemAbilityManager successed." << std::endl; + OHOS::sptr remoteObject_ = sm->CheckSystemAbility(4201); + if (remoteObject_ == nullptr) { + std::cout << "CheckSystemAbility failed." << std::endl; + return -2; + } + + if (!remoteObject_->IsProxyObject()) { + std::cout << "IsProxyObject failed." << std::endl; + return -4; + } + + std::cout << "CheckSystemAbility successed." << std::endl; + proxy_ = OHOS::iface_cast(remoteObject_); + if (proxy_ == nullptr) { + std::cout << "proxy failed." << std::endl; + return -3; + } + int ret = 0; + if (func == 1) { + std::cout << "usb_port_test : << GetPorts >> test begin -> " << std::endl; + std::vector ports; + std::cout << "Create ports successed." << std::endl; + int ret = proxy_->GetPorts(ports); + if (ret) { + std::cout << "GetPorts false." << ret << std::endl; + } + std::cout << "GetPorts successed.ret:" << ret << std::endl; + cout << "size:" <id << endl; + cout << "supportedModes:"<supportedModes << endl; + cout << "currentMode:"<usbPortStatus->currentMode << endl; + cout << "currentPowerRole:"<usbPortStatus->currentPowerRole << endl; + cout << "currentDataRole:"<< ports.at(0)->usbPortStatus->currentDataRole << endl; + } + if (func == 2) { + std::cout << "usb_port_test : << GetSupportedModes" << std::endl; + ret = g_usbClient.GetSupportedModes(1); + std::cout << "usb_device_test : << GetSupportedModes >> ret -> " << ret << std::endl; + } + if (func == 3) { + std::cout << "usb_port_test : << SetPortRole" << std::endl; + ret = g_usbClient.SetPortRole(1, 1, 1); + std::cout << "usb_port_test : << SetPortRole >> ret -> " << ret << std::endl; + } + if (func == 4) { + std::cout << "usb_port_test : << SetPortRole" << std::endl; + ret = g_usbClient.SetPortRole(1, 2, 2); + std::cout << "usb_port_test : << SetPortRole >> ret -> " << ret << std::endl; + } + if (func == 5) { + std::cout << "usb_port_test : << SetPortRole " << std::endl; + ret = g_usbClient.SetPortRole(1, 3, 3); + std::cout << "usb_port_test : << SetPortRole >> ret -> " << ret << std::endl; + } + if (func == 6) { + std::cout << "usb_port_test : << SetPortRole" << std::endl; + ret = g_usbClient.SetPortRole(1, 0, 0); + std::cout << "usb_port_test : << SetPortRole >> ret -> " << ret << std::endl; + } + if (func == 7) { + std::cout << "usb_right_test : << RequestRight" << std::endl; + ret = g_usbClient.RequestRight("aaa"); + std::cout << "usb_right_test : << RequestRight aaa>> ret -> " << ret << std::endl; + } + if (func == 8) { + std::cout << "usb_right_test : << HasRight" << std::endl; + ret = g_usbClient.HasRight("aaa"); + std::cout << "usb_right_test : << HasRight aaa >> ret -> " << ret << std::endl; + } + if (func == 9) { + std::cout << "usb_right_test : << HasRight" << std::endl; + ret = g_usbClient.HasRight("bbb"); + std::cout << "usb_right_test : << HasRight bbb >> ret -> " << ret << std::endl; + } +} diff --git a/services/native/src/usb_serial_reader.cpp b/services/native/src/usb_serial_reader.cpp new file mode 100644 index 00000000..d37fc42b --- /dev/null +++ b/services/native/src/usb_serial_reader.cpp @@ -0,0 +1,21 @@ +#include "usb_serial_reader.h" + +namespace OHOS +{ + namespace USB + { + + UsbSerialReader::UsbSerialReader(SystemAbility *systemAbility, + UsbPermissionManager *permissionManager, std::string serialNumber) { + systemAbility_ = systemAbility; + permissionManager_ = permissionManager; + serialNumber_ = serialNumber; + } + + UsbSerialReader::~UsbSerialReader() + { + } + + } + +} diff --git a/services/native/src/usb_server_event_handler.cpp b/services/native/src/usb_server_event_handler.cpp new file mode 100644 index 00000000..7d46b140 --- /dev/null +++ b/services/native/src/usb_server_event_handler.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_server_event_handler.h" +#include "usb_service.h" +//#include "usb_common.h" + +namespace OHOS { +namespace USB { + +UsbServerEventHandler::UsbServerEventHandler(const std::shared_ptr &runner, + const wptr &service) + : AppExecFwk::EventHandler(runner), service_(service) +{ + //USB_HILOGE(MODULE_USB_SERVICE, "UsbServerEventHandler::UsbServerEventHandler instance created."); +} + +void UsbServerEventHandler::ProcessEvent([[maybe_unused]] const AppExecFwk::InnerEvent::Pointer &event) {} +//TODO get data from event. + std::string deviceAddress; + int deviceClass = 0; + int deviceSubclass = 0; + unsigned char * descriptors = (unsigned char *)"000"; + //service_.usbDeviceAdded(deviceAddress, deviceClass, deviceSubclass, descriptors, 3); + +} // namespace USB +} // namespace OHOS diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp new file mode 100644 index 00000000..831e695b --- /dev/null +++ b/services/native/src/usb_service.cpp @@ -0,0 +1,496 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_service.h" +#include +#include +#include +#include +#include +#include "file_ex.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "usb_common.h" +#include "usb_descriptor_parser.h" +#include "usb_device_manager.h" +#include "usb_permission_manager.h" +#include "usb_port_manager.h" +#include + + +namespace OHOS { +namespace USB { + +namespace { +const std::string USB_SERVICE_NAME = "UsbService"; +constexpr int32_t COMMEVENT_REGISTER_RETRY_TIMES = 10; +constexpr int32_t COMMEVENT_REGISTER_WAIT_DELAY_US = 20000; +const int32_t USB_MAX_DESCRIPTOR_SIZE = 256; +} // namespace + +auto pms = DelayedSpSingleton::GetInstance(); +const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility( + DelayedSpSingleton::GetInstance().GetRefPtr()); + +UsbService::UsbService() : SystemAbility(USB_MANAGER_USB_SERVICE_ID, true) { + usbHostManger_ = new UsbHostManager(nullptr, nullptr); + usbDeviceManager_ = new UsbDeviceManager(); + usbPermissionManager = new UsbPermissionManager(); + usbPortManager = new UsbPortManager(); + //usbPortManager->Init(); + usbFunctionManager = new UsbFunctionManager(); +} +UsbService::~UsbService() { + delete usbHostManger_; + delete usbDeviceManager_; + delete usbPermissionManager; + delete usbPortManager; +} + +int32_t UsbService::OnFoundDevice(FoundDevice &info) { return 0; } + +void UsbService::OnStart() { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d usb_service OnStart enter", __func__, __LINE__); + if (ready_) { + USB_HILOGE(MODULE_USB_SERVICE, "OnStart is ready, nothing to do"); + return; + } + if (!(Init())) { + USB_HILOGE(MODULE_USB_SERVICE, "OnStart call init fail"); + return; + } + if (!(InitUsbd())) { + USB_HILOGE(MODULE_USB_SERVICE, "OnStart call initUsbd fail"); + return; + } + usbPortManager->Init(); // + ready_ = true; + USB_HILOGE(MODULE_USB_SERVICE, "OnStart and add system ability success"); +} + +bool UsbService::Init() { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d usb_service Init enter", __func__, __LINE__); + if (!eventRunner_) { + eventRunner_ = AppExecFwk::EventRunner::Create(USB_SERVICE_NAME); + if (eventRunner_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "Init failed due to create EventRunner"); + return false; + } + } + if (!handler_) { + handler_ = std::make_shared(eventRunner_, pms); + if (handler_ == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "Init failed due to create handler error"); + return false; + } + if (!Publish(pms)) { + USB_HILOGE(MODULE_USB_SERVICE, "OnStart register to system ability manager failed."); + return false; + } + } + while (commEventRetryTimes_ <= COMMEVENT_REGISTER_RETRY_TIMES) { + if (!IsCommonEventServiceAbilityExist()) { + ++commEventRetryTimes_; + usleep(COMMEVENT_REGISTER_WAIT_DELAY_US); + } else { + commEventRetryTimes_ = 0; + break; + } + } + USB_HILOGE(MODULE_USB_SERVICE, "Init success"); + return true; +} + +void UsbService::usbDeviceAdded(std::string deviceAddress, int deviceClass, + int deviceSubclass, unsigned char *descriptors, + unsigned int descriptorsSize) { + usbHostManger_->usbDeviceAdded(deviceAddress, deviceClass, deviceSubclass, + descriptors, descriptorsSize); +} +bool UsbService::InitUsbd() { + usbdSubscriber_ = new UsbServiceSubscriber(); + ErrCode ret = UsbdClient::BindUsbdSubscriber(usbdSubscriber_); + USB_HILOGE(MODULE_USB_SERVICE, "fumang entry InitUsbd ret: %{public}d", ret); + return SUCCEEDED(ret); +} + +void UsbService::OnStop() { + USB_HILOGE(MODULE_USB_SERVICE, "fumang entry stop service %{public}d", ready_); + if (!ready_) { + return; + } + eventRunner_.reset(); + handler_.reset(); + ready_ = false; + UsbdClient::UnbindUsbdSubscriber(); +} + +bool UsbService::IsCommonEventServiceAbilityExist() { + sptr sm = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!sm) { + USB_HILOGE(MODULE_USB_SERVICE, + "IsCommonEventServiceAbilityExist Get ISystemAbilityManager " + "failed, no SystemAbilityManager"); + return false; + } + sptr remote = + sm->CheckSystemAbility(COMMON_EVENT_SERVICE_ABILITY_ID); + if (!remote) { + USB_HILOGE(MODULE_USB_SERVICE, "No CesServiceAbility"); + return false; + } + return true; +} +int32_t UsbService::OpenDevice(uint8_t busNum, uint8_t devAddr) +{ + return UsbdClient::OpenDevice(busNum, devAddr); +} +int32_t UsbService::HasRight(std::string deviceName) { + USB_HILOGI(MODULE_USB_SERVICE, "calling usbPermissionManager HasRight"); + int uid = IPCSkeleton::GetCallingUid(); + USB_HILOGI(MODULE_USB_SERVICE, "HasRight uid = %{public}d", uid); + return usbPermissionManager->HasPermission(deviceName, uid); +} + +int32_t UsbService::RequestRight(std::string deviceName) { + USB_HILOGI(MODULE_USB_SERVICE, "calling usbPermissionManager RequestRight"); + int uid = IPCSkeleton::GetCallingUid(); + USB_HILOGI(MODULE_USB_SERVICE, "RequestRight uid = %{public}d", uid); + return usbPermissionManager->RequestPermission(deviceName, uid); +} + +int32_t UsbService::GetDevices(std::vector &deviceList) +{ + std::map devices; + usbHostManger_->getDevices(devices); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s list size %{public}d", __func__, devices.size()); + for(auto it = devices.begin(); it != devices.end(); ++it) { + deviceList.push_back(*it->second); + } + return ERR_OK; +} + +int32_t UsbService::GetCurrentFunctions() { + return UsbdClient::GetCurrentFunctions(); +} + +bool UsbService::SetCurrentFunctions(int32_t funcs) { + USB_HILOGI(MODULE_USB_SERVICE, "func = %{public}d", funcs); + return UsbdClient::SetCurrentFunctions(funcs); +} + +int32_t UsbService::UsbFunctionsFromString(std::string funcs) { + USB_HILOGI(MODULE_USB_SERVICE, "calling UsbFunctionsFromString"); + return UsbFunctionManager::FromStringFunctions(funcs); +} + +std::string UsbService::UsbFunctionsToString(int32_t funcs) { + USB_HILOGI(MODULE_USB_SERVICE, "calling UsbFunctionsToString"); + return UsbFunctionManager::ToStringFunctions(funcs); +} + +int32_t UsbService::GetPorts(std::vector &result) { + USB_HILOGI(MODULE_USB_SERVICE, "calling usbPortManager getPorts"); + return usbPortManager->GetPorts(result); +} + +int32_t UsbService::GetSupportedModes(int32_t portId, int32_t &result) { + USB_HILOGI(MODULE_USB_SERVICE, "calling usbPortManager getSupportedModes"); + return usbPortManager->GetSupportedModes(portId, result); +} + +int32_t UsbService::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) { + USB_HILOGI(MODULE_USB_SERVICE, "calling usbd getPorts"); + return UsbdClient::SetPortRole(portId, powerRole, dataRole); +} + +int32_t UsbService::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface) +{ + return UsbdClient::ClaimInterface(busNum, devAddr, interface); +} +int32_t UsbService::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface) +{ + return UsbdClient::ReleaseInterface(busNum, devAddr, interface); +} +int32_t UsbService::BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout) +{ + int32_t ret = ERR_INVALID_VALUE; + struct TransferParams params = {interface, endpoint, buffer, length, timeout}; + ret = UsbdClient::BulkTransferRead(busNum, devAddr, params); + return ret; +} +int32_t UsbService::BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t endpoint, const uint8_t *buffer, uint32_t length, int32_t timeout) +{ + int32_t ret = ERR_INVALID_VALUE; + struct TransferParams params = {interface, endpoint, (uint8_t *)buffer, length, timeout}; + ret = UsbdClient::BulkTransferWrite(busNum, devAddr, params); + return ret; +} +int32_t UsbService::ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout) +{ + int32_t ret = ERR_INVALID_VALUE; + struct CtrlTransferParamsParams params = {requestType, request, value, index, buffer, length, timeout}; + ret = UsbdClient::ControlTransfer(busNum, devAddr, params); + return ret; +} +int32_t UsbService::SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) +{ + return UsbdClient::SetConfig(busNum, devAddr, configIndex); +} +int32_t UsbService::GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) +{ + return UsbdClient::GetConfig(busNum, devAddr, configIndex); +} +int32_t UsbService::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) +{ + return UsbdClient::SetInterface(busNum, devAddr, interfaceid, altIndex); +} +int32_t UsbService::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *desc, uint32_t &length) +{ + return UsbdClient::GetRawDescriptor(busNum, devAddr, desc, length); +} +int32_t UsbService::RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointId, const void *clientData, uint32_t clientLength, const uint8_t *buffer, uint32_t length) +{ + int32_t ret = ERR_INVALID_VALUE; + struct RequestParams params = {(void *)clientData, clientLength, (uint8_t *)buffer, length}; + ret = UsbdClient::RequestQueue(busNum, devAddr, ifId, endpointId, params); + return ret; +} +int32_t UsbService::RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, void *clientData, uint32_t &clientLength, uint8_t *buffer, uint32_t &length) +{ + int32_t ret = ERR_INVALID_VALUE; + struct RequestParams params = {clientData, clientLength, buffer, length}; + ret = UsbdClient::RequestWait(busNum, devAddr, timeout, params); + return ret; +} + +int32_t UsbService::RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) +{ + return UsbdClient::RequestCancel(busNum, devAddr, interfaceid, endpointId); +} + +int32_t UsbService::Close(uint8_t busNum, uint8_t devAddr) +{ + return UsbdClient::CloseDevice(busNum, devAddr); +} + +int32_t UsbService::GetDevice(uint8_t busNum, uint8_t devAddr, UsbDevice &devInfo) +{ + MAP_STR_DEVICE deviceList; + usbHostManger_->getDevices(deviceList); + if(deviceList.empty()) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s empty", __func__); + return ERR_INVALID_VALUE; + } + devInfo = *(deviceList.begin()->second); + return ERR_OK; +} + +static std::string GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, uint8_t idx) +{ + int ret = ERR_OK; + uint32_t length = USB_MAX_DESCRIPTOR_SIZE; + uint8_t *buffer = new uint8_t[length]; + std::string string = " "; + struct DescriptorParams params = {idx, buffer, length}; + + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d getString idx:%{public}d", __func__, __LINE__, idx); + if(idx == 0){ + delete [] buffer; + return string; + } + + ret = UsbdClient::GetStringDescriptor(busNum, devAddr, params); + if(ret != ERR_OK){ + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d get string[%{public}d] failed ret:%{public}d", __func__, __LINE__, idx, ret); + delete [] buffer; + return string; + } + if( (length<2) || (buffer[1] != 0x03) ){ + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d type or length error, type:0x%{public}02x(0x03) len:%{public}d", + __func__, __LINE__, buffer[1], length); + delete [] buffer; + return string; + } + uint16_t tbuf[length+1]; + for(uint32_t i = 0; i < length; ++i){ + tbuf[i] = buffer[i+2]; + } + std::wstring wstr((wchar_t *)(tbuf), (length-2)/2); + string=std::string(wstr.begin(), wstr.end()); + + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d getString idx:%{public}d String:%{public}s length:%{public}d", + __func__, __LINE__, idx, string.c_str(), length); + delete [] buffer; + buffer = NULL; + return string; +} + +static std::string BcdToString(uint8_t *bcd, uint32_t bcdLen) +{ + std::string tstr; + for(uint32_t i = 0; i < bcdLen; ++i){ + tstr += std::to_string((bcd[i] & 0xf0) >> 4); + tstr += std::to_string((bcd[i] & 0x0f)); + } + return tstr; +} + +static int32_t FillDevStrings(UsbDevice *dev) +{ + uint8_t busNum; + uint8_t devAddr; + if(dev == NULL){ + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d dev is NULL", __func__, __LINE__); + return ERR_INVALID_VALUE; + } + + busNum = dev->GetBusNum(); + devAddr = dev->GetDevAddr(); + uint16_t bcdDevice = dev->GetbcdDevice(); + dev->SetVersion(BcdToString((uint8_t *)&bcdDevice, 2)); + dev->SetManufacturerName(GetDevStringValFromIdx(busNum, devAddr, dev->GetiManufacturer())); + dev->SetProductName(GetDevStringValFromIdx(busNum, devAddr, dev->GetiProduct())); + dev->SetmSerial(GetDevStringValFromIdx(busNum, devAddr, dev->GetiSerialNumber())); + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d iSerial:%{public}d mSerial:%{public}s Manufactur:%{public}s product:%{public}s version:%{public}s", + __func__, __LINE__, dev->GetiSerialNumber(), dev->GetmSerial().c_str(), dev->GetManufacturerName().c_str(), + dev->GetProductName().c_str(), dev->GetVersion().c_str()); + + std::vector configs; + configs = dev->GetConfigs(); + for(auto it=configs.begin(); it != configs.end(); ++it){ + it->SetName(GetDevStringValFromIdx(busNum, devAddr, it->GetiConfiguration())); + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d Config:%{public}d %{public}s", + __func__, __LINE__, it->GetiConfiguration(), it->GetName().c_str()); + std::vector interfaces = it->GetInterfaces(); + for(auto itIF = interfaces.begin(); itIF != interfaces.end(); ++itIF){ + itIF->SetName(GetDevStringValFromIdx(busNum, devAddr, itIF->GetiInterface())); + USB_HILOGW(MODULE_USB_SERVICE, "%{public}s:%{public}d interface:%{public}d %{public}s", + __func__, __LINE__, itIF->GetiInterface(), itIF->GetName().c_str()); + } + it->SetInterfaces(interfaces); + } + dev->SetConfigs(configs); + + return ERR_OK; +} + +int32_t UsbService::GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice *dev) +{ + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d busNum:%{public}d devAddr:%{public}d", __func__, __LINE__, busNum, devAddr); + int32_t ret = ERR_OK; + uint32_t length = USB_MAX_DESCRIPTOR_SIZE; + uint8_t *buffer = new uint8_t[length]; + //int32_t fd = 0; + if(!dev){ + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d dev is NULL busNum:%{public}d devAddr:%{public}d", __func__, __LINE__, busNum, devAddr); + return ERR_INVALID_VALUE; + } + + ret = UsbdClient::OpenDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d fumang UsbdClient::OpenDevice ret=%{public}d", __func__, __LINE__, ret); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s fumang UsbdClient::OpenDevice failed ret=%{public}d", __func__, ret); + delete [] buffer; + return ret; + } + + ret = UsbdClient::GetDeviceDescriptor(busNum, devAddr, buffer, length); + if(ret != ERR_OK){ + UsbdClient::CloseDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d UsbdClient::GetDeviceDescriptor failed ret=%{public}d busNum:%{public}d devAddr:%{public}d", + __func__, __LINE__, ret, busNum, devAddr); + delete [] buffer; + return ret; + } + dev->SetBusNum(busNum); + dev->SetDevAddr(devAddr); + dev->SetName(std::to_string(busNum) + "-" + std::to_string(devAddr)); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s fumang GetDeviceDescriptor length=%{public}d", __func__, length); + UsbdClient::PrintBuffer("GetDeviceInfo::GetDeviceDescriptor", buffer, length); + ret = UsbDescriptorParser::ParseDeviceDescriptor(buffer, length, *dev); + if(ret != ERR_OK){ + UsbdClient::CloseDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s ParseDeviceDescriptor failed ret=%{public}d", __func__, ret); + delete [] buffer; + return ret; + } + + std::vector configs; + for(uint8_t i = 0; i < dev->GetDescConfigCount(); ++i) { + int32_t cursor = 0; + length = USB_MAX_DESCRIPTOR_SIZE; + struct DescriptorParams params = {i, buffer, length}; + ret = UsbdClient::GetConfigDescriptor(busNum, devAddr, params); + if(ret != ERR_OK){ + UsbdClient::CloseDevice(busNum, devAddr); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s fumang UsbdClient::GetConfigDescriptor failed ret=%{public}d", __func__, ret); + delete [] buffer; + return ret; + } + UsbdClient::PrintBuffer("GetDeviceInfo::GetConfigDescriptor", buffer, length); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s fumang GetConfigDescriptor length=%{public}d", __func__, length); + int32_t configCursor = 0; + USBConfig config; + ret = UsbDescriptorParser::ParseConfigDescriptor(buffer + cursor, length - cursor, configCursor, config); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s ParseConfigDescriptor[%{public}d] ret:%{public}d", __func__, i, ret); + cursor += configCursor; + configs.push_back(config); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s fumang Descriptor=%{public}s", __func__, config.ToString().c_str()); + } + dev->SetConfigs(configs); + ret = FillDevStrings(dev); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d FillDevStrings ret=%{public}d", __func__, __LINE__, ret); + + UsbdClient::CloseDevice(busNum, devAddr); + delete [] buffer; + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s fumang Descriptor=%{public}s", __func__, dev->ToString().c_str()); + + return ERR_OK; +} + +bool UsbService::AddDevice(uint8_t busNum, uint8_t devAddr) +{ + int32_t ret = ERR_OK; + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d busNum:%{public}d devAddr:%{public}d", __func__, __LINE__, busNum, devAddr); + UsbDevice *devInfo = new UsbDevice(); + memset(devInfo, 0, sizeof(UsbDevice)); + ret = GetDeviceInfo(busNum, devAddr, devInfo); + if(ret == ERR_OK) { + usbHostManger_->AddDevice(devInfo); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d devInfo:%{public}s", __func__, __LINE__, devInfo->ToString().c_str()); + return true; + } + delete devInfo; + return false; +} +bool UsbService::DelDevice(uint8_t busNum, uint8_t devAddr) +{ + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d fumang entry", __func__, __LINE__); + int32_t ret = Close(busNum, devAddr); + if(ret != ERR_OK) { + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d Close device failed width ret = %{public}d", __func__, __LINE__, ret); + } + return usbHostManger_->DelDevice(busNum, devAddr); +} + +void UsbService::UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole){ + usbPortManager->UpdatePort( portId, powerRole, dataRole); +} + +} // namespace USB +} // namespace OHOS diff --git a/services/native/src/usb_service_subscriber.cpp b/services/native/src/usb_service_subscriber.cpp new file mode 100644 index 00000000..e05b0e03 --- /dev/null +++ b/services/native/src/usb_service_subscriber.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_service_subscriber.h" + +#include "common_event_data.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "ohos/aafwk/content/want.h" +#include "usb_common.h" +#include "string_ex.h" +#include "sp_singleton.h" +#include "usb_service.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::EventFwk; + +namespace OHOS { +namespace USB { + +UsbServiceSubscriber::UsbServiceSubscriber() {} + +int32_t UsbServiceSubscriber::PortChangedEvent(int32_t portId, int32_t powerRole, int32_t dataRole){ + auto pms = DelayedSpSingleton::GetInstance(); + Want want; + want.SetAction(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED); + pms->UpdateUsbPort(portId, powerRole, dataRole); + CommonEventData data; + data.SetWant(want); + CommonEventPublishInfo publishInfo; + publishInfo.SetOrdered(true); + bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo); + if (!isSuccess) { + USB_HILOGE(MODULE_USB_SERVICE, "failed to publish PortChangedEvent"); + } + return isSuccess; +} + +int32_t UsbServiceSubscriber::DeviceEvent(const UsbInfo &info) +{ + int status = info.getDevInfoStatus(); + int32_t ret = ERR_OK; + Want want; + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d status:%{public}d bus:%{public}d dev:%{public}d", + __func__, __LINE__, status, info.getDevInfoBusNum(), info.getDevInfoDevNum()); + switch(status){ + case ACT_DEVUP: + want.SetAction(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED); + break; + case ACT_DEVDOWN: + want.SetAction(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED); + break; + case ACT_UPDEVICE: + want.SetAction(CommonEventSupport::COMMON_EVENT_USB_ACCESSORY_ATTACHED); + break; + case ACT_DOWNDEVICE: + want.SetAction(CommonEventSupport::COMMON_EVENT_USB_ACCESSORY_DETACHED); + break; + default: + return ret; + } + CommonEventData data; + data.SetWant(want); + CommonEventPublishInfo publishInfo; + publishInfo.SetOrdered(true); + bool isSuccess = CommonEventManager::PublishCommonEvent(data, publishInfo); + if (!isSuccess) { + USB_HILOGE(MODULE_USB_SERVICE, "failed to publish USB_CHANGED event"); + ret = ERR_NO_INIT; + } + if((ACT_UPDEVICE == status) || (ACT_DOWNDEVICE == status)){ + return ret; + } + int busNum = info.getDevInfoBusNum(); + int devAddr = info.getDevInfoDevNum(); + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d status:%{public}d bus:%{public}d dev:%{public}d", + __func__, __LINE__, status, busNum, devAddr); + auto pms = DelayedSpSingleton::GetInstance(); + USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d status:%{public}d pms:%{public}s", + __func__, __LINE__, status, pms==nullptr?"NULL":"OK"); + if(status == ACT_DEVUP) + { + USB_HILOGE(MODULE_USB_SERVICE, "usb attached"); + pms->AddDevice(busNum, devAddr); + }else{ + USB_HILOGE(MODULE_USB_SERVICE, "usb detached"); + pms->DelDevice(busNum, devAddr); + } + + return ret; +} + +int32_t UsbServiceSubscriber::Update(const UsbInfo &info) +{ + return ERR_NO_INIT; +} + + + +} // namespace USB +} // namespace OHOS diff --git a/services/native/src/usb_test.cpp b/services/native/src/usb_test.cpp new file mode 100644 index 00000000..8b9c8183 --- /dev/null +++ b/services/native/src/usb_test.cpp @@ -0,0 +1,543 @@ +#include +#include +#include +#include +#include +#include "string_ex.h" +#include "iremote_object.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include "usb_common.h" +#include "usb_device.h" +#include "iusb_srv.h" +#include "usb_server_proxy.h" +#include "usb_srv_client.h" +#include "usb_request.h" + +void PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length) +{ + std::ostringstream oss; + if(title == NULL || buffer == nullptr || length == 0) { + return; + } + oss.str(""); + oss << title << " << 二进制数据流[" << length << "字节] >> :" ; + for(uint32_t i = 0; i < length; ++i) { + oss << " " << std::hex << (int)buffer[i]; + } + oss << " --> " << buffer << std::endl; + //USB_HILOGD(MODULE_USBD, "%{public}s", oss.str()); + std::cout << oss.str(); +} + +#if 1 +int main(int argc, char** argv) +{ + // if(argc != 2) { + // std::cout << "Usage: usb_device_test server_id busNum devAddr." << std::endl; + // } + //int32_t serviceId = 4201; + int32_t ret; + static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance(); + + std::cout << "versionInfo:" << g_usbClient.GetVersion() << std::endl; +#if 0 + + OHOS::sptr proxy_; + + std::mutex mutex_; + std::lock_guard lock(mutex_); + OHOS::sptr sm = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + std::cout << "GetSystemAbilityManager failed" << std::endl; + return -1; + } + + std::cout << "GetSystemAbilityManager successed." << std::endl; + OHOS::sptr remoteObject_ = sm->CheckSystemAbility(serviceId); + if (remoteObject_ == nullptr) { + std::cout << "CheckSystemAbility failed." << std::endl; + return -2; + } + + if (!remoteObject_->IsProxyObject()) { + std::cout << "IsProxyObject failed." << std::endl; + return -4; + } + + std::cout << "CheckSystemAbility successed." << std::endl; + proxy_ = OHOS::iface_cast(remoteObject_); + if(proxy_ == nullptr) { + std::cout << "proxy failed." << std::endl; + return -3; + } +#endif +// GetDevices + std::cout << "usb_device_test : << GetDevices >> test begin -> " << std::endl; + std::vector deviceList; + ret = g_usbClient.GetDevices(deviceList); + if(ret != 0) { + std::cout << "GetDevices failed width ret = " << ret << std::endl; + return -5; + } + if(deviceList.empty()) { + std::cout << "device list is empty." << std::endl; + return -5; + } + for(auto it = deviceList.begin(); it != deviceList.end(); ++it) { + std::cout << "device info :" << it->ToString() << "\n" << std::endl; + } + std::cout << "usb_device_test : << GetDevices >> test end ok <-" << std::endl; + OHOS::USB::UsbDevice device = deviceList[0]; + std::vector configs = device.GetConfigs(); + OHOS::USB::USBConfig config = configs[0]; + std::vector interfaces = config.GetInterfaces(); + OHOS::USB::UsbInterface interface; + OHOS::USB::USBEndpoint inEp; + OHOS::USB::USBEndpoint outEp; + bool interfaceFlg = false; + bool inEpFlg = false; + bool outEpFlg = false; + std::string hstr = "\t"; + std::cout << "test device Info:" << device.ToString() << std::endl; + std::cout << hstr << "config Info:" << config.ToString() << std::endl; + hstr += "\t"; + for(int32_t i = 0; i < config.GetInterfaceCount(); ++i){ + OHOS::USB::UsbInterface tif = interfaces[i]; + std::cout << hstr << "Interfaces[" << i << "]:" << std::endl; + std::cout << hstr << tif.ToString() << std::endl; + std::string hestr = hstr + "\t"; + std::vector mEndpoints = tif.GetEndpoints(); + for(int32_t j = 0; j < tif.GetEndpointCount(); ++j){ + OHOS::USB::USBEndpoint tep = mEndpoints[j]; + std::cout << hestr << "mEndpoints[" << j << "]:" << std::endl; + std::cout << hestr << tep.ToString() << std::endl; + if((!interfaceFlg) && (tep.GetType() == 2)){ + if(tep.GetDirection()){ + inEp = tep; + inEpFlg = true; + }else{ + outEp = tep; + outEpFlg = true; + } + } + } + if(!interfaceFlg){ + if((outEpFlg) && (inEpFlg)){ + interface = interfaces[i]; + interfaceFlg = true; + }else{ + inEpFlg = false; + outEpFlg = false; + } + } + std::cout << std::endl; + } + #if 0 + interface = interfaces[1]; + interface.GetEndpoint(0, inEp); + interface.GetEndpoint(1, outEp); + interfaceFlg = true; + outEpFlg = true; + inEpFlg = true; + #endif + if(interfaceFlg){ + std::cout << "Use Interface: " << interface.ToString() << std::endl; + } + if(inEpFlg){ + std::cout << "Use inEp: " << inEp.ToString() << std::endl; + } + if(outEpFlg){ + std::cout << "Use outEp: " << outEp.ToString() << std::endl; + } + //return 0; +// OpenDevice +#if 1 + std::cout << "usb_device_test : << OpenDevice >> test begin -> " << std::endl; + OHOS::USB::USBDevicePipe pip; + ret = g_usbClient.OpenDevice(device, pip); + if(ret != 0) { + std::cout << "OpenDevice failed width ret = " << ret << std::endl; + return -5; + } + std::cout << "USBDevicePipe:" + << " busNum = " << (int)(pip.GetBusNum()) + << ", devAddr = " << (int)pip.GetDevAddr() + << ", fd = " << (int)pip.GetFd() + << ", serial = " << pip.GetSerial().c_str(); +#endif + +#if 1 + std::cout << "usb_device_test : << Control Transfer >> " << std::endl; + uint8_t buffer1[255] = {0}; + uint32_t length1 = 255; + memset(buffer1, 0, 255); + int32_t requestCmd = 6; + int32_t requestType = ((1 << 7) | (0 << 5) | (0 & 0x1f)); + //int32_t value = (1 << 8) + 0; // descriptor type << 8 + descriptor index + int32_t value = (2 << 8) + 0; // descriptor type << 8 + descriptor index + int32_t index = 0; + int32_t timeout = 5000; + //ret = pip.ControlTransfer(requestType, requestCmd, value, index, buffer1, length1, timeout); + ret = g_usbClient.ControlTransfer(pip, requestType, requestCmd, value, index, buffer1, length1, timeout); + if(ret != 0) { + std::cout << "control message read failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "control message read success" << std::endl; + PrintBuffer("设备描述符", buffer1, length1); + } +#endif + + //ret = pip.SetConfiguration(config); + //ret = g_usbClient.SetConfiguration(pip, config); + if(ret != 0) { + std::cout << "SetConfiguration failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "SetConfiguration success" << std::endl; + } + + if(interfaceFlg){ + //OHOS::USB::UsbInterface interface = interfaces[1]; + std::cout << "ReleaseInterface InterfaceInfo:" << interface.ToString() << std::endl; + //ret = pip.ReleaseInterface(interface); + ret = g_usbClient.ReleaseInterface(pip, interface); + if(ret != 0) { + std::cout << "ReleaseInterface failed width ret = " << ret << std::endl; + } else { + std::cout << "ReleaseInterface success" << std::endl; + } + + std::cout << "ClaimInterface InterfaceInfo:" << interface.ToString() << std::endl; + //ret = pip.ClaimInterface(interface, true); + ret = g_usbClient.ClaimInterface(pip, interface, true); + if(ret != 0) { + std::cout << "ClaimInterface failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "ClaimInterface success" << std::endl; + } + } + + +// Control read +#if 1 + if(outEpFlg){ + OHOS::USB::UsbRequest sendReq; + uint8_t tag[50] = "queue write"; + sendReq.SetClientData((void *)tag, 11); + sendReq.Initialize(pip, outEp); + //memcpy_s(buffer1, length1, "usb request test !!!", length1); + memset(buffer1, 0, 255); + length1 = 55; + //char *str2 = "usb request test !!!"; + memcpy_s(buffer1, length1, "usb request write test !!!", 20); + ret = sendReq.Queue(buffer1, length1); + std::cout << "request queue write ret = " << ret << " leng: " << length1 << std::endl; + if(ret != 0) { + std::cout << "request queue write failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "request queue write success" << std::endl; + } + + OHOS::USB::UsbRequest resultReq; + //ret = pip.RequestWait(5000, resultReq); + memset(buffer1, 0, 255); + length1 = 52; + ret = g_usbClient.PipeRequestWait(pip, 5000, resultReq); + if(ret != 0) { + std::cout << "request wait write failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "request wait write success len:" << resultReq.GetLength() << " ClientLength:" << resultReq.GetClientLength() << std::endl; + PrintBuffer("request write result", resultReq.GetBuffer(), resultReq.GetLength()); + PrintBuffer("request write data", (const uint8_t *)resultReq.GetClientData(), resultReq.GetClientLength()); + } + } + + if(inEpFlg){ + OHOS::USB::UsbRequest sendReq; + uint8_t tag[50] = "queue read"; + sendReq.SetClientData((void *)tag, 10); + sendReq.Initialize(pip, inEp); + //memcpy_s(buffer1, length1, "usb request test !!!", length1); + memset(buffer1, 0, 255); + length1 = 255; + //char *str2 = "usb request test !!!"; + memcpy_s(buffer1, length1, "Queue request read test !!!", 20); + length1 = 250; + ret = sendReq.Queue(buffer1, length1); + std::cout << "request queue read ret = " << ret << " leng: " << length1 << std::endl; + if(ret != 0) { + std::cout << "request queue read failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "request queue read success" << std::endl; + } + + OHOS::USB::UsbRequest resultReq; + //ret = pip.RequestWait(5000, resultReq); + ret = g_usbClient.PipeRequestWait(pip, 5000, resultReq); + if(ret != 0) { + std::cout << "request read wait failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "request read wait success len:" << resultReq.GetLength() << " ClientLength:" << resultReq.GetClientLength() << std::endl; + PrintBuffer("request read buffer", resultReq.GetBuffer(), resultReq.GetLength()); + PrintBuffer("request read data", (const uint8_t *)resultReq.GetClientData(), resultReq.GetClientLength()); + } + } + +#endif + +#if 1 +// BulkTransfer + if(interfaceFlg){ + std::cout << "usb_device_test : << Bulk transfer start >> " << std::endl; + //OHOS::USB::USBEndpoint outEp; + //interface.GetEndpoint(1, outEp); + if(outEpFlg){ + std::cout << "usb_device_test : << Bulk transfer write >> " << std::endl; + uint8_t buffer[50] = "hello world 123456789"; + uint32_t length = 50; + ret = g_usbClient.BulkTransfer(pip, outEp, buffer, length, 5000); + if(ret != 0) { + std::cout << "Bulk transfer write failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "Bulk transfer write success" << std::endl; + //PrintBuffer("", buffer, length); + } + } + + // BulkTransfer read + //OHOS::USB::USBEndpoint inEp; + //interface.GetEndpoint(0, inEp); + if(inEpFlg){ + std::cout << "usb_device_test : << Bulk transfer read >> " << std::endl; + memset(buffer1, 0, 255); + length1 = 255; + ret = g_usbClient.BulkTransfer(pip, inEp, buffer1, length1, 5000); + if(ret != 0) { + std::cout << "Bulk transfer read failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "Bulk transfer read success" << std::endl; + PrintBuffer("Bulk read data:", buffer1, length1); + } + } + } +#endif + + +// CloseDevice + std::cout << "usb_device_test : << Close Device >> " << std::endl; + //ret = pip.Close(); + ret = g_usbClient.Close(pip); + if(ret == 0) { + std::cout << "Close device failed width ret = " << ret << std::endl; + return -5; + } else { + std::cout << "Close device success" << std::endl; + } + + return 0; +} +#endif + +#if 0 + +int main(int argc, char** argv) +{ + // if(argc != 2) { + // std::cout << "Usage: usb_device_test server_id busNum devAddr." << std::endl; + // } + int32_t serviceId = 4201; + int32_t ret; + static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance(); + OHOS::sptr proxy_; + + std::mutex mutex_; + std::lock_guard lock(mutex_); + OHOS::sptr sm = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + std::cout << "GetSystemAbilityManager failed" << std::endl; + return -1; + } + std::cout << "GetSystemAbilityManager successed." << std::endl; + OHOS::sptr remoteObject_ = sm->CheckSystemAbility(serviceId); + if (remoteObject_ == nullptr) { + std::cout << "CheckSystemAbility failed." << std::endl; + return -2; + } + + if (!remoteObject_->IsProxyObject()) { + std::cout << "IsProxyObject failed." << std::endl; + return -4; + } + + std::cout << "CheckSystemAbility successed." << std::endl; + proxy_ = OHOS::iface_cast(remoteObject_); + if(proxy_ == nullptr) { + std::cout << "proxy failed." << std::endl; + return -3; + } + +// GetDevices + std::cout << "usb_device_test : << GetDevices >> test begin -> " << std::endl; + std::vector deviceList; + ret = g_usbClient.GetDevices(deviceList); + if(ret != 0) { + std::cout << "GetDevices failed width ret = " << ret << std::endl; + return -5; + } + if(deviceList.empty()) { + std::cout << "device list is empty." << std::endl; + return -5; + } + // for(auto it = deviceList.begin(); it != deviceList.end(); ++it) { + // std::cout << "device info :" << it->ToString().c_str() << std::endl; + // } + // std::cout << "usb_device_test : << GetDevices >> test end ok <-" << std::endl; + OHOS::USB::UsbDevice device = *deviceList.begin(); + OHOS::USB::USBConfig config; + std::vector interfaces; + device.GetConfig(0, config); + if(config.GetInterfaceCount() == 0) { + std::cout << " interface empty" << std::endl; + return -5; + } + + for(int32_t i = 0; i < config.GetInterfaceCount(); ++i) { + OHOS::USB::UsbInterface interface; + config.GetInterface(i, interface); + interfaces.push_back(interface); + std::cout << " interface info[" << i << "] :" << interface.ToString().c_str() << std::endl; + for(int32_t j = 0; j < interface.GetEndpointCount(); ++j) { + OHOS::USB::USBEndpoint ep; + interface.GetEndpoint(j, ep); + std::cout << " endpoint info [" << j << "]:" << ep.ToString().c_str() << std::endl; + } + } +// OpenDevice +#if 1 + std::cout << "usb_device_test : << OpenDevice >> test begin -> " << std::endl; + OHOS::USB::USBDevicePipe pip; + ret = g_usbClient.OpenDevice(device, pip); + if(ret != 0) { + std::cout << "OpenDevice failed width ret = " << ret << std::endl; + return -5; + } + std::cout << "USBDevicePipe:" + << " busNum = " << (int)(pip.GetBusNum()) + << ", devAddr = " << (int)pip.GetDevAddr() + << ", fd = " << (int)pip.GetFd() + << ", serial = " << pip.GetSerial().c_str(); +#endif + std::cout << "usb_device_test : << Bulk transfer write>> " << std::endl; + OHOS::USB::UsbInterface interface = interfaces[0]; + ret = pip.ReleaseInterface(interface); + if(ret != 0) { + std::cout << "ReleaseInterface failed width ret = " << ret << std::endl; + } else { + std::cout << "ReleaseInterface success" << std::endl; + } + + ret = pip.ClaimInterface(interface, true); + if(ret != 0) { + std::cout << "ClaimInterface failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "ClaimInterface success" << std::endl; + } +#if 1 +// BulkTransfer + OHOS::USB::USBEndpoint outEp; + interface.GetEndpoint(1, outEp); + uint8_t buffer[50] = "hello world 123456789"; + uint32_t length = 50; + ret = pip.BulkTransfer(outEp, buffer, length, 5000); + if(ret != 0) { + std::cout << "Bulk transfer write failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "Bulk transfer write success" << std::endl; + //PrintBuffer("", buffer, length); + } + +// BulkTransfer read + OHOS::USB::USBEndpoint inEp; + interface.GetEndpoint(0, inEp); + std::cout << "usb_device_test : << Bulk transfer read >> " << std::endl; + uint8_t buffer1[50] = {}; + uint32_t length1 = 50; + ret = pip.BulkTransfer(inEp, buffer1, length1, 5000); + if(ret != 0) { + std::cout << "Bulk transfer read failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "Bulk transfer read success" << std::endl; + PrintBuffer("Bulk read data:", buffer1, length1); + } +#endif +// Control read +#if 0 + std::cout << "usb_device_test : << Control Transfer >> " << std::endl; + uint8_t buffer1[255] = {}; + uint32_t length1 = 255; + memset(buffer1, 0, 255); + int32_t requestCmd = 6; + int32_t requestType = ((1 << 7) | (0 << 5) | (0 & 0x1f)); + //int32_t value = (1 << 8) + 0; // descriptor type << 8 + descriptor index + int32_t value = (2 << 8) + 0; // descriptor type << 8 + descriptor index + int32_t index = 0; + int32_t timeout = 5000; + ret = pip.ControlTransfer(requestType, requestCmd, value, index, buffer1, length1, timeout); + if(ret != 0) { + std::cout << "control message read failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "control message read success" << std::endl; + PrintBuffer("设备描述符", buffer1, length1); + } + + OHOS::USB::UsbRequest sendReq; + sendReq.Initialize(pip, inEp); + memcpy_s(buffer1, length1, "usb request test !!!", length1); + ret = sendReq.Queue(buffer1, length1); + if(ret != 0) { + std::cout << "request queue failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "request queue success" << std::endl; + } + + OHOS::USB::UsbRequest resultReq; + ret = pip.RequestWait(5000, resultReq); + if(ret != 0) { + std::cout << "request wait failed width ret = " << ret << std::endl; + //return -5; + } else { + std::cout << "request wait success" << std::endl; + PrintBuffer("request result", resultReq.GetBuffer(), resultReq.GetLength()); + } +#endif +// CloseDevice + std::cout << "usb_device_test : << Close Device >> " << std::endl; + ret = pip.Close(); + if(ret != 0) { + std::cout << "Close device failed width ret = " << ret << std::endl; + return -5; + } else { + std::cout << "Close device success" << std::endl; + } + + return 0; +} + +#endif + diff --git a/services/native/test/BUILD.gn b/services/native/test/BUILD.gn new file mode 100644 index 00000000..377eedea --- /dev/null +++ b/services/native/test/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2021 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. + +import("//build/test.gni") + +#################################group######################################### +group("usb_service_unittest") { + testonly = true + deps = [ "unittest/common:unittest", ] + +} +############################################################################### \ No newline at end of file diff --git a/services/native/test/unittest/common/BUILD.gn b/services/native/test/unittest/common/BUILD.gn new file mode 100644 index 00000000..4e56bd0c --- /dev/null +++ b/services/native/test/unittest/common/BUILD.gn @@ -0,0 +1,166 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") +import("//build/test.gni") + +module_output_path = "usb_manager_native/usb_service_test" + +############################################################################### +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "include", + "//utils/system/safwk/native/include", + "//base/usb/usb_manager/interfaces/innerkits/native/include", + ] +} + +##############################unittest##########################################= + +ohos_unittest("test_usbport") { + module_out_path = module_output_path + sources = [ + "src/usb_port_service_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_usbpipe") { + module_out_path = module_output_path + sources = [ + "src/usb_pipe_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_usbrequest") { + module_out_path = module_output_path + sources = [ + "src/usb_request_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +ohos_unittest("test_usbcore") { + module_out_path = module_output_path + sources = [ + "src/usb_core_test.cpp" + ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "${usb_manager_path}/interfaces/innerkits:usbsrv_client", + "${usb_manager_path}/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + "${usb_manager_path}/hdi/client:usbd_client", + ] + + external_deps = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_innerkits", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", + ] +} +############################################################################### +group("unittest") { + testonly = true + deps = [ + ":test_usbport", + ":test_usbpipe", + ":test_usbrequest", + ":test_usbcore" + ] +} +############################################################################### diff --git a/services/native/test/unittest/common/include/usb_controltransfer_test.h b/services/native/test/unittest/common/include/usb_controltransfer_test.h new file mode 100644 index 00000000..ef1d6f66 --- /dev/null +++ b/services/native/test/unittest/common/include/usb_controltransfer_test.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_UsbControlstansferTest_TEST_H +#define USB_UsbControlstansferTest_TEST_H + +#include + +class UsbControlstansferTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USB_UsbControlstansferTest_TEST_H \ No newline at end of file diff --git a/services/native/test/unittest/common/include/usb_core_test.h b/services/native/test/unittest/common/include/usb_core_test.h new file mode 100644 index 00000000..9534d15e --- /dev/null +++ b/services/native/test/unittest/common/include/usb_core_test.h @@ -0,0 +1,13 @@ +#ifndef USB_CORE_TEST_H +#define USB_CORE_TEST_H + +#include + +class UsbCoreTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif \ No newline at end of file diff --git a/services/native/test/unittest/common/include/usb_device_pipe_test.h b/services/native/test/unittest/common/include/usb_device_pipe_test.h new file mode 100644 index 00000000..d6e40041 --- /dev/null +++ b/services/native/test/unittest/common/include/usb_device_pipe_test.h @@ -0,0 +1,13 @@ +#ifndef USB_DEVICE_PIPE_TEST_H +#define USB_DEVICE_PIPE_TEST_H + +#include + +class UsbDevicePipeTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USB_SERVICE_TEST_H \ No newline at end of file diff --git a/services/native/test/unittest/common/include/usb_function_service_test.h b/services/native/test/unittest/common/include/usb_function_service_test.h new file mode 100644 index 00000000..93b26d58 --- /dev/null +++ b/services/native/test/unittest/common/include/usb_function_service_test.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_SERVICE_TEST_H +#define USB_SERVICE_TEST_H + +#include + +class UsbFunctionServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USB_SERVICE_TEST_H diff --git a/services/native/test/unittest/common/include/usb_pipe_test.h b/services/native/test/unittest/common/include/usb_pipe_test.h new file mode 100644 index 00000000..ef1d6f66 --- /dev/null +++ b/services/native/test/unittest/common/include/usb_pipe_test.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_UsbControlstansferTest_TEST_H +#define USB_UsbControlstansferTest_TEST_H + +#include + +class UsbControlstansferTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USB_UsbControlstansferTest_TEST_H \ No newline at end of file diff --git a/services/native/test/unittest/common/include/usb_request_test.h b/services/native/test/unittest/common/include/usb_request_test.h new file mode 100644 index 00000000..b788da1e --- /dev/null +++ b/services/native/test/unittest/common/include/usb_request_test.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_UsbRequestTest_TEST_H +#define USB_UsbRequestTest_TEST_H + +#include + +class UsbRequestTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; +#endif // USB_UsbRequestTest_TEST_H diff --git a/services/native/test/unittest/common/include/usb_service_test.h b/services/native/test/unittest/common/include/usb_service_test.h new file mode 100644 index 00000000..f9bdc645 --- /dev/null +++ b/services/native/test/unittest/common/include/usb_service_test.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_SERVICE_TEST_H +#define USB_SERVICE_TEST_H + +#include + +class UsbServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); + bool IsUsbSupported(); +}; +#endif // USB_SERVICE_TEST_H \ No newline at end of file diff --git a/services/native/test/unittest/common/src/usb_controltransfer_test.cpp b/services/native/test/unittest/common/src/usb_controltransfer_test.cpp new file mode 100644 index 00000000..aff56dd1 --- /dev/null +++ b/services/native/test/unittest/common/src/usb_controltransfer_test.cpp @@ -0,0 +1,460 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_controltransfer_test.h" +#include "usb_service.h" +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_srv_client.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include +// #include "gtest.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbControlstansferTest::SetUpTestCase(void) +{ +} + +void UsbControlstansferTest::TearDownTestCase(void) +{ +} + +void UsbControlstansferTest::SetUp(void) +{ +} + +void UsbControlstansferTest::TearDown(void) +{ +} + + +/** + * @tc.name: Usbcontrolstansfer001 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F(UsbControlstansferTest,Usbcontrolstansfer001,TestSize.Level1) +{ + uint32_t len =10; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000000 ,5 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer001 end."); +} + +/** + * @tc.name: Usbcontrolstansfer002 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer002,TestSize.Level1) +{ + uint32_t len =0; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,5 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer002 end."); +} + +/** + * @tc.name: Usbcontrolstansfer003 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer003,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000010 ,5 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer003 end."); +} + +/** + * @tc.name: Usbcontrolstansfer004 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer004,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000000 ,2 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer004 end."); +} + +/** + * @tc.name: Usbcontrolstansfer005 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer005,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000000 ,0X08 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer005 end."); +} + +/** + * @tc.name: Usbcontrolstansfer006 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer006,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000001 ,9 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer006 end."); +} + +/** + * @tc.name: Usbcontrolstansfer007 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F(UsbControlstansferTest,Usbcontrolstansfer007,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000000 ,6 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer007 end."); +} + +/** + * @tc.name: Usbcontrolstansfer008 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer008,TestSize.Level1) +{ + uint32_t len =0; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000001 ,6 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer008 end."); +} + +/** + * @tc.name: Usbcontrolstansfer009 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer009,TestSize.Level1) +{ + uint32_t len =16; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000010 ,6 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer009 end."); +} + +/** + * @tc.name: Usbcontrolstansfer0010 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer0010,TestSize.Level1) +{ + uint32_t len =32; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000000 ,0X0A ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer0010 end."); +} + +/** + * @tc.name: Usbcontrolstansfer011 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer011,TestSize.Level1) +{ + uint32_t len =16; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000000 ,3 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer011 end."); +} + +/** + * @tc.name: Usbcontrolstansfer012 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer012,TestSize.Level1) +{ + USBDevicePipe ctrl; + uint32_t len =8; + uint32_t &length =len; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000000 ,0 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer012 end."); +} + +/** + * @tc.name: Usbcontrolstansfer013 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F(UsbControlstansferTest,Usbcontrolstansfer013,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000000 ,4 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer013 end."); +} + +/** + * @tc.name: Usbcontrolstansfer014 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer014,TestSize.Level1) +{ + uint32_t len =0; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,4 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer014 end."); +} + +/** + * @tc.name: Usbcontrolstansfer015 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer015,TestSize.Level1) +{ + uint32_t len =16; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000010 ,4 ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer015 end."); +} + +/** + * @tc.name: Usbcontrolstansfer016 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer016,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0X00; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,1 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer016 end."); +} + +/** + * @tc.name: Usbcontrolstansfer017 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer017,TestSize.Level1) +{ + uint32_t len =8; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0xFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000010 ,0X0C ,0 ,0 ,buff ,length ,0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer017 end."); +} + +/** + * @tc.name: Usbcontrolstansfer018 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer018,TestSize.Level1) +{ + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0XFF; + uint32_t len =32; + uint32_t &length =len; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0XFFFF ,5 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer018 end."); +} + +/** + * @tc.name: Usbcontrolstansfer019 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer019,TestSize.Level1) +{ + uint32_t len =32; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0XFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,0X0B ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer019 end."); +} + +/** + * @tc.name: Usbcontrolstansfer020 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer020,TestSize.Level1) +{ + uint32_t len =32; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0XFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,2 ,0X7FFFFFFF ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer020 end."); +} + +/** + * @tc.name: Usbcontrolstansfer021 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer021,TestSize.Level1) +{ + uint32_t len =0; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0XFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,7 ,0 ,0b11111111 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer021 end."); +} + +/** + * @tc.name: Usbcontrolstansfer022 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer022,TestSize.Level1) +{ + uint32_t len =32; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0XFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,0 ,0 ,0 ,buff ,length ,0X7FFFFFFF); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer022 end."); +} + +/** + * @tc.name: Usbcontrolstansfer023 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer023,TestSize.Level1) +{ + uint32_t len =128; + uint32_t &length =len; + USBDevicePipe ctrl; + uint8_t *buff = (uint8_t*)0XFF; + auto& CtrlTransfer = UsbSrvClient::GetInstance(); + auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,8 ,0 ,0 ,buff ,length ,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + ASSERT_TRUE(ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer023 end."); +} diff --git a/services/native/test/unittest/common/src/usb_core_test.cpp b/services/native/test/unittest/common/src/usb_core_test.cpp new file mode 100644 index 00000000..b53d0daa --- /dev/null +++ b/services/native/test/unittest/common/src/usb_core_test.cpp @@ -0,0 +1,483 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_core_test.h" +#include +#include "usb_srv_client.h" +#include "usb_service.h" +// #include "usb_core.h" +#include +#include "hilog_wrapper.h" +#include "iservice_registry.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" +#include "string_ex.h" +#include +#include + +using namespace testing::ext; +using namespace OHOS::USB; +using namespace OHOS; +using namespace std; + +void UsbCoreTest::SetUpTestCase(void) +{ +} + +void UsbCoreTest::TearDownTestCase(void) +{ +} + +void UsbCoreTest::SetUp(void) +{ +} + +void UsbCoreTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbCoreTest001 + * @tc.desc: Test functions of getDevices + * @tc.desc: int32_t GetDevices(std::vector &deviceList) + * @tc.desc: 正向测试:代码正常运行,正确获取deviceList + * @tc.type: FUNC + */ +HWTEST_F (UsbCoreTest,getDevices001,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices001 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices001 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::getDevices001 end."); +} + +/** + * @tc.name: getDevices002 + * @tc.desc: Test functions to getDevices(std::vector &deviceList); + * @tc.type: FUNC + */ +HWTEST_F (UsbCoreTest,getDevices002,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices002 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices002 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.at(1); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices002 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::getDevices002 end."); +} + +/** + * @tc.name: getDevices003 + * @tc.desc: Test functions to getDevices(std::vector &deviceList); + * @tc.type: FUNC + */ +HWTEST_F (UsbCoreTest,getDevices003,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices003 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices003 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.at(2); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getDevices003 %{public}d ret=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::getDevices003 end."); +} + + +/** + * @tc.name: GetCurrentFunctions004 + * @tc.desc: Test functions to GetCurrentFunctions() + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, GetCurrentFunctions004, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + int32_t funcs = 0; + funcs = instance.GetCurrentFunctions(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::funcs=%{public}d", funcs); + ASSERT_TRUE(funcs == 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::GetCurrentFunctions004 end."); +} + +/** + * @tc.name: SetCurrentFunctions005 + * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, SetCurrentFunctions005, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + bool isok = instance.SetCurrentFunctions(UsbFunctionManager::FUNCTION_HDC); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions=%{public}d", isok); + ASSERT_TRUE(isok); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions005 end."); +} + +/** + * @tc.name: SetCurrentFunctions006 + * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, SetCurrentFunctions006, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + bool isok = instance.SetCurrentFunctions(UsbFunctionManager::FUNCTION_NONE); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions=%{public}d", isok); + ASSERT_TRUE(isok); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions006 end."); +} + +/** + * @tc.name: SetCurrentFunctions007 + * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, SetCurrentFunctions007, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + bool isok = instance.SetCurrentFunctions(UsbFunctionManager::FUNCTION_ECM); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions=%{public}d", isok); + ASSERT_TRUE(isok); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions007 end."); +} + +/** + * @tc.name: SetCurrentFunctions008 + * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, SetCurrentFunctions008, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + bool isok = instance.SetCurrentFunctions(UsbFunctionManager::FUNCTION_ACM); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions=%{public}d", isok); + ASSERT_TRUE(isok); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions008 end."); +} + +/** + * @tc.name: SetCurrentFunctions009 + * @tc.desc: Test functions to SetCurrentFunctions(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbFunctionService009, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + bool isok = instance.SetCurrentFunctions(-1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions=%{public}d", isok); + ASSERT_TRUE(isok); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::SetCurrentFunctions009 end."); +} + +/** + * @tc.name: UsbFunctionsFromString010 + * @tc.desc: Test functions to UsbFunctionsFromString(string funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbFunctionsFromString010, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + int32_t funcCode = instance.UsbFunctionsFromString(UsbFunctionManager::FUNCTION_NAME_HDC); + USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::functionsFromString=%{public}d", funcCode); + ASSERT_TRUE(funcCode); + USB_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionsFromString010 end."); +} + +/** + * @tc.name: UsbFunctionsToString011 + * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbFunctionsToString011, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + std::string funcName = instance.UsbFunctionsToString(UsbFunctionManager::FUNCTION_NONE); + USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str()); + ASSERT_TRUE(funcName.empty()); + USB_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionsToString011 end."); +} + +/** + * @tc.name: UsbFunctionsToString012 + * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbFunctionsToString012, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + std::string funcName = instance.UsbFunctionsToString(UsbFunctionManager::FUNCTION_HDC); + USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str()); + ASSERT_TRUE(!(funcName.empty())); + USB_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionsToString012 end."); +} + +/** + * @tc.name: UsbFunctionsToString013 + * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbFunctionsToString013, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + std::string funcName = instance.UsbFunctionsToString(UsbFunctionManager::FUNCTION_ACM); + USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str()); + ASSERT_TRUE(!(funcName.empty())); + USB_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionsToString013 end."); +} + +/** + * @tc.name: UsbFunctionsToString014 + * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbFunctionsToString014, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + std::string funcName = instance.UsbFunctionsToString(UsbFunctionManager::FUNCTION_ECM); + USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str()); + ASSERT_TRUE(!(funcName.empty())); + USB_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionsToString014 end."); +} + +/** + * @tc.name: UsbFunctionsToString015 + * @tc.desc: Test functions to UsbFunctionsToString(int32_t funcs) + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbFunctionsToString015, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + std::string funcName = instance.UsbFunctionsToString(-1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::UsbFunctionsToString=%{public}s", funcName.c_str()); + ASSERT_TRUE(!(funcName.empty())); + USB_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionsToString015 end."); +} + +// /** +// * @tc.name: UsbOpenDevice016 +// * @tc.desc: Test functions of OpenDevice +// * @tc.desc: int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pip) +// * @tc.desc: 正向测试:代码正常运行,返回结果为0 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, UsbOpenDevice016, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::vector deviceList; + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbOpenDevice016 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbOpenDevice016 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(deviceList[0],pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::OpenDevice=%{public}d", ret); + ASSERT_TRUE(ret == 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::OpenDevice end."); +} + +/** + * @tc.name: UsbOpenDevice017 + * @tc.desc: Test functions of OpenDevice + * @tc.desc: int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pip) + * @tc.desc: 反向测试:代码正常运行,返回结果不为0 + * @tc.type: FUNC + */ +HWTEST_F(UsbCoreTest, UsbOpenDevice017, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::vector deviceList; + auto ret = UsbSrvClient.GetDevices(deviceList); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbOpenDevice017 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(deviceList.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbOpenDevice017 %{public}d size=%{public}d",__LINE__ ,deviceList.size()); + USBDevicePipe pipe; + ret = UsbSrvClient.OpenDevice(deviceList[0],pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbOpenDevice017 OpenDevice=%{public}d", ret); + ASSERT_TRUE(ret != 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbCoreTest::UsbOpenDevice017 OpenDevice end."); +} + +// /** +// * @tc.name: UsbHasRight018 +// * @tc.desc: Test functions of HasRight +// * @tc.desc: int32_t HasRight(std::string deviceName) +// * @tc.desc: 正向测试:代码正常运行,返回结果为0 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, UsbHasRight018, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::string deviceName = "device_01"; + int32_t result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight018 HasRight=%{public}d", result); + ASSERT_TRUE(result == 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbHasRight018 HasRight end."); +} + +// /** +// * @tc.name: UsbHasRight019 +// * @tc.desc: Test functions of HasRight +// * @tc.desc: int32_t HasRight(std::string deviceName) +// * @tc.desc: 反向测试:代码正常运行,返回结果为1 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, UsbHasRight019, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::string deviceName = "device_01"; + int32_t result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight019 HasRight=%{public}d", result); + ASSERT_TRUE(result == 0); + result = UsbSrvClient.RequestRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight019 RequestRight=%{public}d", result); + ASSERT_TRUE(result == 1); + result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight019 HasRight=%{public}d", result); + ASSERT_TRUE(result == 1); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbHasRight019 HasRight end."); +} + +// /** +// * @tc.name: UsbHasRight020 +// * @tc.desc: Test functions of HasRight +// * @tc.desc: int32_t HasRight(std::string deviceName) +// * @tc.desc: 反向测试:代码正常运行,返回结果为1 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, UsbHasRight020, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::string deviceName = "device_01"; + int32_t result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight020 HasRight=%{public}d", result); + ASSERT_TRUE(result == 0); + result = UsbSrvClient.RequestRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight020 RequestRight=%{public}d", result); + ASSERT_TRUE(result == 0); + result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight020 HasRight=%{public}d", result); + ASSERT_TRUE(result == 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbHasRight020 HasRight end."); +} + +// /** +// * @tc.name: UsbHasRight021 +// * @tc.desc: Test functions of HasRight +// * @tc.desc: int32_t HasRight(std::string deviceName) +// * @tc.desc: 反向测试:代码正常运行,返回结果为1 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, UsbHasRight021, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::string deviceName = "device_01"; + int32_t result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight021 HasRight=%{public}d", result); + ASSERT_TRUE(result == 1); + result = UsbSrvClient.RequestRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight021 RequestRight=%{public}d", result); + ASSERT_TRUE(result == 0); + result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight021 HasRight=%{public}d", result); + ASSERT_TRUE(result == 1); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbHasRight021 HasRight end."); +} + +// /** +// * @tc.name: Usbrequestright022 +// * @tc.desc: Test functions of requestright +// * @tc.desc: int32_t requestright(std::string deviceName) +// * @tc.desc: 正向测试:代码正常运行,返回结果为1 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, Usbrequestright022, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::string deviceName = "device_01"; + int32_t result = UsbSrvClient.RequestRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright022 RequestRight=%{public}d", result); + ASSERT_TRUE(result == 1); + result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright022 HasRight=%{public}d", result); + ASSERT_TRUE(result == 1); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::Usbrequestright022 HasRight end."); +} +// /** +// * @tc.name: Usbrequestright023 +// * @tc.desc: Test functions of requestright +// * @tc.desc: int32_t requestright(std::string deviceName) +// * @tc.desc: 正向测试:代码正常运行,返回结果为1 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, Usbrequestright023, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::string deviceName = "device_01"; + int32_t result = UsbSrvClient.RequestRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright023 RequestRight=%{public}d", result); + ASSERT_TRUE(result == 0); + result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright023 HasRight=%{public}d", result); + ASSERT_TRUE(result == 1); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::Usbrequestright023 HasRight end."); +} + +// /** +// * @tc.name: Usbrequestright024 +// * @tc.desc: Test functions of requestright +// * @tc.desc: int32_t requestright(std::string deviceName) +// * @tc.desc: 反向测试:代码报错,返回结果为1 +// * @tc.type: FUNC +// */ +HWTEST_F(UsbCoreTest, Usbrequestright024, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + std::string deviceName = "device_01"; + int32_t result = UsbSrvClient.RequestRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright024 RequestRight=%{public}d", result); + ASSERT_TRUE(result == 0); + result = UsbSrvClient.HasRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright024 HasRight=%{public}d", result); + ASSERT_TRUE(result == 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::Usbrequestright024 HasRight end."); +} + +// TODO 以下两个接口可能会被删除 +// bool AddFunction(std::string functions, std::string function) +// bool RemoveFunction(std::string functions, std::string function) + +// TODO 以下接口暂未实现 +// function setHdcEnabled(enable: boolean): boolean; \ No newline at end of file diff --git a/services/native/test/unittest/common/src/usb_device_pipe_test.cpp b/services/native/test/unittest/common/src/usb_device_pipe_test.cpp new file mode 100644 index 00000000..445bad07 --- /dev/null +++ b/services/native/test/unittest/common/src/usb_device_pipe_test.cpp @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_device_pipe_test.h" +#include +#include "usb_srv_client.h" +#include "usb_service.h" +#include "iservice_registry.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" +#include "string_ex.h" +#include + +using namespace testing::ext; +using namespace OHOS::USB; +using namespace OHOS; +using namespace std; + +void UsbDevicePipeTest::SetUpTestCase(void) +{ +} + +void UsbDevicePipeTest::TearDownTestCase(void) +{ +} + +void UsbDevicePipeTest::SetUp(void) +{ +} + +void UsbDevicePipeTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbService001 + * @tc.desc: Test functions of claimInterface + * @tc.desc: int32_t UsbService::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface) + * @tc.desc: 正向测试:参数在边界值内 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService001, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:通过接口获取interface + UsbInterface interface; + bool force = true; + auto claimInterface = UsbSrvClient.ClaimInterface(interface, force); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::claimInterface=%{public}d", claimInterface); + ASSERT_TRUE(claimInterface); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe001 end."); +} + +/** + * @tc.name: UsbService002 + * @tc.desc: Test functions of claimInterface + * @tc.desc: int32_t UsbService::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface) + * @tc.desc: 反向测试:参数在边界值外 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService002, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:自定义参数,系统未能识别的interface + std::vector mEndpoints; + UsbInterface interface(1,1,1,1,1,"1",mEndpoints); + UsbInterface &itf = interface; + bool force = true; + auto claimInterface = UsbSrvClient.ClaimInterface(itf, force); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::claimInterface=%{public}d", claimInterface); + ASSERT_TRUE(claimInterface); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe002 end."); +} + +/** + * @tc.name: UsbService003 + * @tc.desc: Test functions of releaseInterface + * @tc.desc: 正向测试:参数在边界值内 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService003, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:通过接口获取interface + UsbInterface interface; + auto releaseInterface = UsbSrvClient.ReleaseInterface(interface); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::releaseInterface=%{public}d", releaseInterface); + ASSERT_TRUE(releaseInterface); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe003 end."); +} + +/** + * @tc.name: UsbService004 + * @tc.desc: Test functions of releaseInterface + * @tc.desc: 反向测试:参数在边界值外 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService004, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:自定义interface,系统未能识别的interface + std::vector mEndpoints; + UsbInterface interface(1,1,1,1,1,"1",mEndpoints); + UsbInterface &itf = interface; + auto releaseInterface = UsbSrvClient.ReleaseInterface(itf); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::releaseInterface=%{public}d", releaseInterface); + ASSERT_TRUE(releaseInterface); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe004 end."); +} + +/** + * @tc.name: UsbService005 + * @tc.desc: Test functions of GetFileDescriptor + * @tc.desc: 正向测试:返回结果在边界值内 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService005, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto getFileDescriptor = UsbSrvClient.GetFileDescriptor(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::getFileDescriptor=%{public}d", getFileDescriptor); + // TODO 验证返回结果是否正确 + ASSERT_TRUE(getFileDescriptor); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe005 end."); +} + +/** + * @tc.name: UsbService007 + * @tc.desc: Test functions of RequestWait + * @tc.desc: 正向测试: + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService007, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义 + int64_t timeout = 500; + UsbRequest request; + UsbRequest &req = request; + auto usbRequest = UsbSrvClient.RequestWait(timeout, req); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::usbRequest=%{public}d", usbRequest); + // TODO 验证返回结果 + ASSERT_TRUE(usbRequest); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe007 end."); +} + +/** + * @tc.name: UsbService009 + * @tc.desc: Test functions of SetConfiguration + * @tc.desc: 正向测试:参数在边界值内 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService009, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:参数通过接口获取 + USBConfig config; + bool setConfiguration = UsbSrvClient.SetConfiguration(config); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::setConfiguration=%{public}d", setConfiguration); + ASSERT_TRUE(setConfiguration); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe009 end."); +} + +/** + * @tc.name: UsbService010 + * @tc.desc: Test functions of SetConfiguration + * @tc.desc: 反向测试:参数在边界值外 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService010, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:自定义参数 + USBConfig config; + USBConfig &usbConfig = config; + bool setConfiguration = UsbSrvClient.SetConfiguration(usbConfig); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::setConfiguration=%{public}d", setConfiguration); + ASSERT_TRUE(setConfiguration); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe010 end."); +} + +/** + * @tc.name: UsbService011 + * @tc.desc: Test functions of SetInterface + * @tc.desc: 正向测试:参数在边界值内 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService011, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:参数通过接口获取 + UsbInterface interface; + bool setInterface = UsbSrvClient.SetInterface(interface); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::setInterface=%{public}d", setInterface); + ASSERT_TRUE(setInterface); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe011 end."); +} + +/** + * @tc.name: UsbService012 + * @tc.desc: Test functions of SetInterface + * @tc.desc: 正向测试:参数在边界值内 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService012, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:自定义参数 + UsbInterface interface; + bool setInterface = UsbSrvClient.SetInterface(interface); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::setInterface=%{public}d", setInterface); + ASSERT_TRUE(setInterface); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe012 end."); +} + +/** + * @tc.name: UsbService013 + * @tc.desc: Test functions of GetRawDescriptors + * @tc.desc: 正向测试:参数在边界值内 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService013, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:从OS中获取参数 + uint8_t *buffer = nullptr; + int32_t length = 1; + int32_t getRawDescriptors = UsbSrvClient.GetRawDescriptors(buffer, length); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::getRawDescriptors=%{public}d", getRawDescriptors); + ASSERT_TRUE(getRawDescriptors); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe013 end."); +} + +/** + * @tc.name: UsbService014 + * @tc.desc: Test functions of GetRawDescriptors + * @tc.desc: 反向测试:参数在边界值外 + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService014, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + // TODO 参数定义:自定义参数 + uint8_t len =1; + uint8_t *buffer = &len; + int32_t length = 1; + int32_t getRawDescriptors = UsbSrvClient.GetRawDescriptors(buffer, length); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::getRawDescriptors=%{public}d", getRawDescriptors); + ASSERT_TRUE(getRawDescriptors); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbDevicePipe014 end."); +} + + +/** + * @tc.name: UsbService015 + * @tc.desc: Test functions of close + * @tc.desc: 正向测试: + * @tc.type: FUNC + */ +HWTEST_F (UsbDevicePipeTest, UsbService015, TestSize.Level1) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + bool result = true; + UsbSrvClient.Close(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbDevicePipeTest::close=%{public}d", result); + // TODO 验证返回结果 + ASSERT_TRUE(true); + USB_HILOGD(MODULE_USB_SERVICE, "UsDevicePipe::UsbService015 end."); +} + + +// TODO public String getSerial() +// TODO public boolean openByInt(String, int) \ No newline at end of file diff --git a/services/native/test/unittest/common/src/usb_function_service_test.cpp b/services/native/test/unittest/common/src/usb_function_service_test.cpp new file mode 100644 index 00000000..75a6818f --- /dev/null +++ b/services/native/test/unittest/common/src/usb_function_service_test.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_function_service_test.h" +#include +#include +#include "if_system_ability_manager.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "sys_param.h" +#include "system_ability_definition.h" +#include "usb_common.h" +#include "usb_service.h" +#include "usb_srv_client.h" +#include "usb_function_manager.h" + +using namespace testing::ext; +using namespace OHOS::PowerMgr; +using namespace OHOS; +using namespace std; + +void UsbFunctionServiceTest::SetUpTestCase(void) {} + +void UsbFunctionServiceTest::TearDownTestCase(void) {} + +void UsbFunctionServiceTest::SetUp(void) {} + +void UsbFunctionServiceTest::TearDown(void) {} + +/** + * @tc.name: UsbFunctionService001 + * @tc.desc: Test functions to get status of Present + * @tc.type: FUNC + */ +HWTEST_F(UsbFunctionServiceTest, UsbFunctionService001, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + int32_t funcs = instance.GetCurrentFunctions(); + POWER_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::funcs=%{public}d", funcs); + ASSERT_TRUE(funcs); + POWER_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionService001 end."); +} + +/** + * @tc.name: UsbFunctionService002 + * @tc.desc: Test functions to get status of UsbFunctionService + * @tc.type: FUNC + */ +HWTEST_F(UsbFunctionServiceTest, UsbFunctionService002, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + bool isok = instance.SetCurrentFunctions(UsbFunctionManager::FUNCTION_HDC); + POWER_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::setCurrentFunctions=%{public}d", isok); + ASSERT_TRUE(isok == true); + POWER_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionService002 end."); +} + +/** + * @tc.name: UsbFunctionService003 + * @tc.desc: Test functions to get value of UsbFunctionService + * @tc.type: FUNC + */ +HWTEST_F(UsbFunctionServiceTest, UsbFunctionService003, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + int32_t funcCode = instance.UsbFunctionsFromString(UsbFunctionManager::FUNCTION_NAME_HDC); + POWER_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::functionsFromString=%{public}d", funcCode); + ASSERT_TRUE(funcCode); + POWER_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionService003 end."); +} + +/** + * @tc.name: UsbFunctionService004 + * @tc.desc: Test functions of UsbFunctionService + * @tc.type: FUNC + */ +HWTEST_F(UsbFunctionServiceTest, UsbFunctionService004, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + std::string funcName = instance.UsbFunctionsToString(UsbFunctionManager::FUNCTION_HDC); + POWER_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::functionsToString=%{public}s", funcName); + ASSERT_TRUE(funcName); + POWER_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionService004 end."); +} + +/** + * @tc.name: UsbFunctionService005 + * @tc.desc: Test functions of UsbFunctionService + * @tc.type: FUNC + */ +HWTEST_F(UsbFunctionServiceTest, UsbFunctionService005, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + auto isok = instance.AddFunction("hdc", "none"); + POWER_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::addFunction=%{public}d", isok); + ASSERT_TRUE(isok == true); + POWER_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionService005 end."); +} + +/** + * @tc.name: UsbFunctionService006 + * @tc.desc: Test functions of UsbFunctionService + * @tc.type: FUNC + */ +HWTEST_F(UsbFunctionServiceTest, UsbFunctionService006, TestSize.Level1) { + auto &instance = UsbSrvClient::GetInstance(); + auto isok = instance.RemoveFunction("hdc", "none"); + POWER_HILOGI(MODULE_USB_SERVICE, "UsbFunctionServiceTest::removeFunction=%{public}d", isok); + ASSERT_TRUE(isok == true); + POWER_HILOGD(MODULE_USB_SERVICE, "UsbFunctionService::UsbFunctionService006 end."); +} diff --git a/services/native/test/unittest/common/src/usb_pipe_test.cpp b/services/native/test/unittest/common/src/usb_pipe_test.cpp new file mode 100644 index 00000000..2877a3e9 --- /dev/null +++ b/services/native/test/unittest/common/src/usb_pipe_test.cpp @@ -0,0 +1,1504 @@ +/* + * Copyright (c) 2021 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. + */ +#include "usb_pipe_test.h" +#include "usb_service.h" +#include "usb_common.h" +#include "sp_singleton.h" +#include "hilog_wrapper.h" +#include "usb_srv_client.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include +#include +// #include "gtest.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + +void UsbControlstansferTest::SetUpTestCase(void) +{ +} + +void UsbControlstansferTest::TearDownTestCase(void) +{ +} + +void UsbControlstansferTest::SetUp(void) +{ +} + +void UsbControlstansferTest::TearDown(void) +{ +} + + +/** + * @tc.name: Usbcontrolstansfer001 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F(UsbControlstansferTest,Usbcontrolstansfer001,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontroltransfer001 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontroltransfer001 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer001 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = pipe.ControlTransfer(0b00000000,1,1,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontroltransfer001 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer001 end."); +} + +/** + * @tc.name: Usbcontrolstansfer002 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer002,TestSize.Level1) +{ + // uint32_t len =0; + // uint32_t &length =len; + // USBDevicePipe ctrl; + // uint8_t *buff = (uint8_t*)0X00; + // auto& CtrlTransfer = UsbSrvClient::GetInstance(); + // auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000001 ,5 ,0 ,0 ,buff ,length ,1); + // USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + // ASSERT_TRUE(ret); + // USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer002 end."); + + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer002 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer002 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer002 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = pipe.ControlTransfer(0b00000001,1,1,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer002 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer002 end."); +} + +/** + * @tc.name: Usbcontrolstansfer003 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer003,TestSize.Level1) +{ + // uint32_t len =8; + // uint32_t &length =len; + // USBDevicePipe ctrl; + // uint8_t *buff = (uint8_t*)0xFF; + // auto& CtrlTransfer = UsbSrvClient::GetInstance(); + // auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b00000010 ,5 ,0 ,0 ,buff ,length ,0); + // USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + // ASSERT_TRUE(ret); + + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer003 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer003 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer003 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[255] = {0}; + ret = pipe.ControlTransfer(0b00000010,1,1,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer003 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer003 end."); +} + +/** + * @tc.name: Usbcontrolstansfer004 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer004,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer004 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer004 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer004 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 1; + const USBConfig& config = device.GetConfigs().front(); + uint8_t configId = config.GetId(); + ret = pipe.ControlTransfer(0b10000000,8,0,0,&configId,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer004 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer004 end."); +} + +/** + * @tc.name: Usbcontrolstansfer005 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer005,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer005 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer005 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer005 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 8; + uint8_t buffer[255] = {}; + memset(buffer,0,255); + ret = pipe.ControlTransfer(0b10000000,6,0x100,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer005 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer005 end."); +} + +/** + * @tc.name: Usbcontrolstansfer006 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer006,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer006 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer006 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + UsbInterface interface = device.GetConfigs().front().GetInterfaces().front(); + int32_t intercafeidex = interface.GetiInterface(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer006 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 8; + uint8_t *buffer = (uint8_t *)interface.GetAlternateSetting(); + ret = pipe.ControlTransfer(0b10000001,0X0A,0,intercafeidex,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer006 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer006 end."); +} + +/** + * @tc.name: Usbcontrolstansfer007 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F(UsbControlstansferTest,Usbcontrolstansfer007,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer007 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer007 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer007 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 2; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b10000000,0,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer007 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer007 end."); +} + +/** + * @tc.name: Usbcontrolstansfer008 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer008,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer008 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer008 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer008 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 2; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b10000001,0,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer008 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer008 end."); +} + +/** + * @tc.name: Usbcontrolstansfer009 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer009,TestSize.Level1) +{ + // uint32_t len =16; + // uint32_t &length =len; + // USBDevicePipe ctrl; + // uint8_t *buff = (uint8_t*)0xFF; + // auto& CtrlTransfer = UsbSrvClient::GetInstance(); + // auto ret = CtrlTransfer.ControlTransfer(ctrl ,0b10000010 ,6 ,0 ,0 ,buff ,length ,0); + // USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ret=%{public}d", ret); + // ASSERT_TRUE(ret); + + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer009 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer009 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer009 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 2; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b10000010,0,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer009 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer009 end."); +} + +/** + * @tc.name: Usbcontrolstansfer0010 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer0010,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer0010 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer0010 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + uint8_t addr = device.GetDevAddr(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer010 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[255] = {}; + ret = pipe.ControlTransfer(0b00000000,5,addr,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer0010 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer0010 end."); +} + +/** + * @tc.name: Usbcontrolstansfer011 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer011,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer011 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer011 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer011 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[255] = {}; + const USBConfig& config = device.GetConfigs().front(); + uint8_t configId = config.GetId(); + ret = pipe.ControlTransfer(0b00000000,9,configId,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer011 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer011 end."); +} + +/** + * @tc.name: Usbcontrolstansfer012 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer012,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer012 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer012 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer012 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 8; + uint8_t buffer[255] = {}; + memset(buffer,0,255); + ret = pipe.ControlTransfer(0b00000000,7,0X100,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer012 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer012 end."); +} + +/** + * @tc.name: Usbcontrolstansfer013 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F(UsbControlstansferTest,Usbcontrolstansfer013,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer013 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer013 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer013 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b00000000,3,3,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer013 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer013 end."); +} + +/** + * @tc.name: Usbcontrolstansfer014 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer014,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer014 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer014 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer014 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b00000001,3,3,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer014 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer014 end."); +} + +/** + * @tc.name: Usbcontrolstansfer015 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer015,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer015 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer015 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer015 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b00000010,3,3,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer015 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer015 end."); +} + +/** + * @tc.name: Usbcontrolstansfer016 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer016,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer016 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer016 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + UsbInterface interface = device.GetConfigs().front().GetInterfaces().front(); + int32_t alter = interface.GetAlternateSetting(); + int32_t interfaceindex = interface.GetiInterface(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer016 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b00000001,0X0B,alter,interfaceindex,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer016 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer016 end."); +} + +/** + * @tc.name: Usbcontrolstansfer017 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer017,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer017 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer017 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer017 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 2; + uint8_t buffer[] = {0}; + int idex = device.GetConfigs().front().GetInterfaces().front().GetEndpoints().front().GetInterfaceId(); + ret = pipe.ControlTransfer(0b10000010,0X0C,0,idex,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Usbcontrolstansfer017 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer017 end."); +} + +/** + * @tc.name: Usbcontrolstansfer018 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer018,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer018 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer018 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer018 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0XFFFF,5,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer018 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer018 end."); +} + +/** + * @tc.name: Usbcontrolstansfer019 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer019,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer019 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer019 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer019 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b00000001,11,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer019 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer019 end."); +} + +/** + * @tc.name: Usbcontrolstansfer020 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer020,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer020 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer020 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer020 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b00000001,11,0X7FFFFFFF,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer020 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer020 end."); +} + +/** + * @tc.name: Usbcontrolstansfer021 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer021,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer021 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer021 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer021 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b10000000,7,0,0b11111111,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer021 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer021 end."); +} + +/** + * @tc.name: Usbcontrolstansfer022 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer022,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer022 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer022 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer022 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 100; + uint8_t *buffer = nullptr; + ret = pipe.ControlTransfer(0b00000001,0,0,0,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer022 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer022 end."); +} + +/** + * @tc.name: Usbcontrolstansfer023 + * @tc.desc: Test functions to ControlTransfer + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,UsbControlstansfer023,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer023 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer023 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer023 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t buffer[10] = {0}; + ret = pipe.ControlTransfer(0b00000001,0,0,0,buffer,len,0); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer022 %{public}d ControlTransfer=%{public}d",__LINE__ ,ret); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbcontrolstansfer023 end."); +} + + +/** + * @tc.name: Usbcontrolstansfer024 + * @tc.desc: Test functions to ClaimInterface(const UsbInterface &interface, bool force); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,ClaimInterface024,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer024 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer024 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer024 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = devi.front().GetConfigs().front().GetInterfaces().front(); + ret = pipe.ClaimInterface(interface,true); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::UsbControlstansfer024 %{public}d ClaimInterface=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::UsbControlstansfer024 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbControlstansfer024 end."); + +} + +/** + * @tc.name: ClaimInterface025 + * @tc.desc: Test functions to ClaimInterface(const UsbInterface &interface, bool force); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,ClaimInterface025,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface025 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface025 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface025 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = devi.front().GetConfigs().front().GetInterfaces().front(); + ret = pipe.ClaimInterface(interface,false); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface025 %{public}d ClaimInterface=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::ClaimInterface025 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::ClaimInterface025 end."); + +} + +/** + * @tc.name: ClaimInterface026 + * @tc.desc: Test functions to ClaimInterface(const UsbInterface &interface, bool force); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,ClaimInterface026,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface026 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface026 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface026 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = devi.front().GetConfigs().front().GetInterfaces().at(1); + ret = pipe.ClaimInterface(interface,true); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface026 %{public}d ClaimInterface=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::ClaimInterface026 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::ClaimInterface026 end."); + +} + +/** + * @tc.name: ClaimInterface027 + * @tc.desc: Test functions to ClaimInterface(const UsbInterface &interface, bool force); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,ClaimInterface027,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface027 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface027 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface027 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = devi.front().GetConfigs().front().GetInterfaces().at(1); + ret = pipe.ClaimInterface(interface,false); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface027 %{public}d ClaimInterface=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::ClaimInterface027 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::ClaimInterface027 end."); + +} + +/** + * @tc.name: ClaimInterface028 + * @tc.desc: Test functions to ClaimInterface(const UsbInterface &interface, bool force); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,ClaimInterface028,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface028 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface028 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface028 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = devi.front().GetConfigs().front().GetInterfaces().at(8); + ret = pipe.ClaimInterface(interface,true); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ClaimInterface028 %{public}d ClaimInterface=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::ClaimInterface028 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::ClaimInterface028 end."); +} + +/** + * @tc.name: ReleaseInterface029 + * @tc.desc: Test functions to ReleaseInterface(const UsbInterface &interface); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Usbcontrolstansfer029,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface029 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface029 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface029 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = devi.front().GetConfigs().front().GetInterfaces().front(); + ret = pipe.ReleaseInterface(interface); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface029 %{public}d ReleaseInterface=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::ReleaseInterface029 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::ReleaseInterface029 end."); +} + +/** + * @tc.name: ReleaseInterface030 + * @tc.desc: Test functions to ReleaseInterface(const UsbInterface &interface); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,ReleaseInterface030,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface030 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface030 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface030 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = devi.front().GetConfigs().front().GetInterfaces().at(1); + ret = pipe.ReleaseInterface(interface); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::ReleaseInterface030 %{public}d ReleaseInterface=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::ReleaseInterface030 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::ReleaseInterface030 end."); +} + +/** + * @tc.name: BulkTransfer031 + * @tc.desc: Test functions to BulkTransfer(const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,BulkTransfer031,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer031 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer031 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer031 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + USBEndpoint point = devi.front().GetConfigs().front().GetInterfaces().front().GetEndpoints().front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer031 %{public}d point=%{public}d",__LINE__ ,ret); + uint8_t buffer[255] = {}; + uint32_t len = 255; + memset(buffer,0,len); + ret = pipe.BulkTransfer(point,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer031 %{public}d BulkTransfer=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::BulkTransfer031 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::BulkTransfer031 end."); +} + +/** + * @tc.name: BulkTransfer032 + * @tc.desc: Test functions to BulkTransfer(const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,BulkTransfer032,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer032 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer032 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer032 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + USBEndpoint point = devi.front().GetConfigs().front().GetInterfaces().at(2).GetEndpoints().at(3); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer032 %{public}d point=%{public}d",__LINE__ ,ret); + uint8_t buffer[64] = {}; + uint32_t len = 64; + memset(buffer,0,len); + ret = pipe.BulkTransfer(point,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer032 %{public}d BulkTransfer=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::BulkTransfer032 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::BulkTransfer032 end."); +} + +/** + * @tc.name: BulkTransfer033 + * @tc.desc: Test functions to BulkTransfer(const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,BulkTransfer033,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer033 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer033 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer033 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + USBEndpoint point = devi.front().GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer033 %{public}d point=%{public}d",__LINE__ ,ret); + uint8_t buffer[255] = {}; + uint32_t len = 515; + memset(buffer,0,len); + ret = pipe.BulkTransfer(point,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer033 %{public}d BulkTransfer=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::BulkTransfer033 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::BulkTransfer033 end."); +} + +/** + * @tc.name: BulkTransfer034 + * @tc.desc: Test functions to BulkTransfer(const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,BulkTransfer034,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer034 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer034 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer034 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + USBEndpoint point = devi.front().GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer034 %{public}d point=%{public}d",__LINE__ ,ret); + uint8_t buffer[255] = {}; + uint32_t len = 1; + memset(buffer,0,len); + ret = pipe.BulkTransfer(point,buffer,len,500); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer034 %{public}d BulkTransfer=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::BulkTransfer034 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::BulkTransfer034 end."); +} + +/** + * @tc.name: BulkTransfer035 + * @tc.desc: Test functions to BulkTransfer(const USBEndpoint &endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,BulkTransfer035,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer035 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer035 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer035 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + USBEndpoint point = devi.front().GetConfigs().front().GetInterfaces().at(1).GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer035 %{public}d point=%{public}d",__LINE__ ,ret); + uint8_t buffer[255] = {}; + uint32_t len = 255; + memset(buffer,0,len); + ret = pipe.BulkTransfer(point,buffer,len,-1); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::BulkTransfer035 %{public}d BulkTransfer=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::BulkTransfer035 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::BulkTransfer035 end."); +} + +/** + * @tc.name: GetRawDescriptors036 + * @tc.desc: Test functions to GetRawDescriptors(uint8_t *buffer, uint32_t &length); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,GetRawDescriptors036,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors036 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors036 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors036 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 255; + uint8_t *buffer = new uint8_t(255); + ret = pipe.GetRawDescriptors(buffer,len); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors036 %{public}d GetRawDescriptors=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::GetRawDescriptors036 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::GetRawDescriptors036 end."); +} + +/** + * @tc.name: GetRawDescriptors037 + * @tc.desc: Test functions to GetRawDescriptors(uint8_t *buffer, uint32_t &length); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,GetRawDescriptors037,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors037 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors037 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors037 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 255; + uint8_t *buffer = nullptr; + ret = pipe.GetRawDescriptors(buffer,len); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors037 %{public}d GetRawDescriptors=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::GetRawDescriptors037 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::GetRawDescriptors037 end."); +} + +/** + * @tc.name: GetRawDescriptors038 + * @tc.desc: Test functions to GetRawDescriptors(uint8_t *buffer, uint32_t &length); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,GetRawDescriptors038,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors038 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors038 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors038 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 0; + uint8_t *buffer = new uint8_t(255); + ret = pipe.GetRawDescriptors(buffer,len); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors038 %{public}d GetRawDescriptors=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::GetRawDescriptors038 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::GetRawDescriptors038 end."); +} + +/** + * @tc.name: GetRawDescriptors039 + * @tc.desc: Test functions to GetRawDescriptors(uint8_t *buffer, uint32_t &length); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,GetRawDescriptors039,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors039 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors039 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors039 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + uint32_t len = 1; + uint8_t *buffer = new uint8_t(1); + ret = pipe.GetRawDescriptors(buffer,len); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetRawDescriptors039 %{public}d GetRawDescriptors=%{public}d",__LINE__ ,ret); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::GetRawDescriptors039 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::GetRawDescriptors039 end."); +} + +// /** +// * @tc.name: GetFileDescriptor040 +// * @tc.desc: Test functions to GetFileDescriptor(); +// * @tc.type: FUNC +// */ +// HWTEST_F (UsbControlstansferTest,GetFileDescriptor040,TestSize.Level1) +// { +// vector devi; +// auto& UsbSrvClient = UsbSrvClient::GetInstance(); +// auto ret = UsbSrvClient.GetDevices(devi); +// ASSERT_TRUE(ret == ERR_OK); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor040 %{public}d ret=%{public}d",__LINE__ ,ret); +// ASSERT_TRUE(!(devi.empty())) << "delist NULL"; +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor040 %{public}d size=%{public}d",__LINE__ ,devi.size()); +// USBDevicePipe pipe; +// UsbDevice device = devi.front(); +// ret = UsbSrvClient.OpenDevice(device,pipe); +// ASSERT_TRUE(ret == ERR_OK); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor040 %{public}d ret=%{public}d",__LINE__ ,ret); +// ret = pipe.GetFileDescriptor(); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor040 %{public}d fd=%{public}d",__LINE__ ,ret); +// bool close = UsbSrvClient.Close(pipe); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::GetFileDescriptor040 %{public}d close=%{public}d",__LINE__, close); +// EXPECT_TRUE(close); +// USB_HILOGD(MODULE_USB_SERVICE, "UsbService::GetFileDescriptor040 end."); +// } + +/** + * @tc.name: SetInterface041 + * @tc.desc: Test functions to SetInterface(const UsbInterface &interface); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,SetInterface041,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface041 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface041 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface041 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = device.GetConfigs().front().GetInterfaces().front(); + ret = pipe.SetInterface(interface); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface041 %{public}d SetInterface=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::SetInterface041 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::SetInterface041 end."); +} + +/** + * @tc.name: SetInterface042 + * @tc.desc: Test functions to SetInterface(const UsbInterface &interface); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,SetInterface042,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface042 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface042 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface042 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = device.GetConfigs().at(1).GetInterfaces().at(0); + ret = pipe.SetInterface(interface); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface042 %{public}d SetInterface=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::SetInterface042 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::SetInterface042 end."); +} + +/** + * @tc.name: SetInterface043 + * @tc.desc: Test functions to SetInterface(const UsbInterface &interface); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,SetInterface043,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface043 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface043 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface043 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + UsbInterface interface = device.GetConfigs().at(1).GetInterfaces().at(1); + ret = pipe.SetInterface(interface); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetInterface043 %{public}d SetInterface=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::SetInterface043 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::SetInterface043 end."); +} + +/** + * @tc.name: SetConfiguration044 + * @tc.desc: Test functions to SetConfiguration(const USBConfig &config); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,SetConfiguration044,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration044 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration044 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration044 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + USBConfig config = device.GetConfigs().front(); + ret = pipe.SetConfiguration(config); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration044 %{public}d SetConfiguration=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::SetConfiguration044 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::SetConfiguration044 end."); +} + +/** + * @tc.name: SetConfiguration045 + * @tc.desc: Test functions to SetConfiguration(const USBConfig &config); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,SetConfiguration045,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration045 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration045 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration045 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + USBConfig config = device.GetConfigs().at(1); + ret = pipe.SetConfiguration(config); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::SetConfiguration045 %{public}d SetConfiguration=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::SetConfiguration045 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::SetConfiguration045 end."); +} + +/** + * @tc.name: getSerial046 + * @tc.desc: Test functions to getSerial(); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,getSerial046,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial046 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial046 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial046 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + std::string serial = pipe.GetSerial(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial047 %{public}d Serial=%{public}s",__LINE__ ,serial.c_str()); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::getSerial046 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::getSerial046 end."); +} + +/** + * @tc.name: getSerial047 + * @tc.desc: Test functions to getSerial(); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,getSerial047,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial047 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial047 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.at(1); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial047 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + std::string serial = pipe.GetSerial(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::getSerial047 %{public}d Serial=%{public}s",__LINE__ ,serial.c_str()); + bool close = UsbSrvClient.Close(pipe); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::getSerial047 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::getSerial047 end."); +} + +// /** +// * @tc.name: GetFileDescriptor048 +// * @tc.desc: Test functions to GetFileDescriptor(); +// * @tc.type: FUNC +// */ +// HWTEST_F (UsbControlstansferTest,GetFileDescriptor048,TestSize.Level1) +// { +// vector devi; +// auto& UsbSrvClient = UsbSrvClient::GetInstance(); +// auto ret = UsbSrvClient.GetDevices(devi); +// ASSERT_TRUE(ret == ERR_OK); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor048 %{public}d ret=%{public}d",__LINE__ ,ret); +// ASSERT_TRUE(!(devi.empty())) << "delist NULL"; +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor048 %{public}d size=%{public}d",__LINE__ ,devi.size()); +// USBDevicePipe pipe; +// UsbDevice device = devi.at(1); +// ret = UsbSrvClient.OpenDevice(device,pipe); +// ASSERT_TRUE(ret == ERR_OK); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor048 %{public}d ret=%{public}d",__LINE__ ,ret); +// ret = pipe.GetFileDescriptor(); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor048 %{public}d fd=%{public}d",__LINE__ ,ret); +// bool close = UsbSrvClient.Close(pipe); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::GetFileDescriptor048 %{public}d close=%{public}d",__LINE__, close); +// EXPECT_TRUE(close); +// USB_HILOGD(MODULE_USB_SERVICE, "UsbService::GetFileDescriptor048 end."); +// } + +/** + * @tc.name: Close049 + * @tc.desc: Test functions to Close(); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Close049,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Close049 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Close049 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor049 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + ret = pipe.Close(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Close049 %{public}d close=%{public}d",__LINE__, ret); + EXPECT_TRUE(ret == ERR_OK); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Close049 end."); +} + +/** + * @tc.name: Close050 + * @tc.desc: Test functions to Close(); + * @tc.type: FUNC + */ +HWTEST_F (UsbControlstansferTest,Close050,TestSize.Level1) +{ + vector devi; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(devi); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Close050 %{public}d ret=%{public}d",__LINE__ ,ret); + ASSERT_TRUE(!(devi.empty())) << "delist NULL"; + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::Close050 %{public}d size=%{public}d",__LINE__ ,devi.size()); + USBDevicePipe pipe; + UsbDevice device = devi.front(); + ret = UsbSrvClient.OpenDevice(device,pipe); + ASSERT_TRUE(ret == ERR_OK); + USB_HILOGI(MODULE_USB_SERVICE, "UsbControlstansferTest::GetFileDescriptor050 %{public}d OpenDevice=%{public}d",__LINE__ ,ret); + ret = pipe.Close(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Close049 %{public}d close=%{public}d",__LINE__, ret); + EXPECT_TRUE(ret == ERR_OK); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Close050 end."); +} \ No newline at end of file diff --git a/services/native/test/unittest/common/src/usb_port_service_test.cpp b/services/native/test/unittest/common/src/usb_port_service_test.cpp new file mode 100644 index 00000000..ea99139b --- /dev/null +++ b/services/native/test/unittest/common/src/usb_port_service_test.cpp @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2021 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. + */ +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include "usb_srv_client.h" +#include "usb_service.h" + +using namespace testing::ext; +using namespace OHOS::USB; +using namespace OHOS; + + +class UsbPortServiceTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void UsbPortServiceTest::SetUpTestCase(void) { + // step 2: input testsuit setup step +} + +void UsbPortServiceTest::TearDownTestCase(void) { + // step 2: input testsuit teardown step +} + +void UsbPortServiceTest::SetUp(void) { + // step 3: input testcase setup step +} + +void UsbPortServiceTest::TearDown(void) { + // step 3: input testcase teardown step +} + +/** + * @tc.name: UsbPortService01 + * @tc.desc: Test UsbPortService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbPortServiceTest, UsbPortServiceTest01, TestSize.Level1) { + auto &usbSrvClient = UsbSrvClient::GetInstance(); + std::vector portlist; + // USB_HILOGD(MODULE_USB_SERVICE, "Get usbSrvClient &=%{public}d", (int)usbSrvClient); + auto ports = usbSrvClient.GetPorts(portlist); + ASSERT_TRUE(ports == 0); + USB_HILOGD(MODULE_USB_SERVICE, "Get UsbPort size=%{public}d", ports); +} + +/** + * @tc.name: UsbPortService02 + * @tc.desc: Test UsbPortService Get. + * @tc.type: FUNC + */ +HWTEST_F (UsbPortServiceTest, UsbPortService02, TestSize.Level0) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto modes = UsbSrvClient.GetSupportedModes(0); + USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", modes); + ASSERT_TRUE(modes != 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest02 end."); +} + +/** + * @tc.name: UsbPortService03 + * @tc.desc: Test GetPorts + * @tc.type: FUNC + */ +HWTEST_F (UsbPortServiceTest, UsbPortService03, TestSize.Level0) +{ + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto modes = UsbSrvClient.GetSupportedModes(-1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", modes); + ASSERT_TRUE(modes != 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest03 end."); +} + +// /** +// * @tc.name: usb_port_service_test01 +// * @tc.desc: Verify the function +// * @tc.type: FUNC +// * @tc.require: AR00000000 SR00000000 +// */ +// HWTEST_F(UsbPortServiceTest, UsbPortServiceTest01, TestSize.Level1) { +// auto &usbSrvClient = UsbSrvClient::GetInstance(); +// // USB_HILOGD(MODULE_USB_SERVICE, "Get usbSrvClient &=%{public}d", (int)usbSrvClient); +// auto ports = usbSrvClient.GetPorts(); +// ASSERT_TRUE(ports.size() != 0); +// USB_HILOGD(MODULE_USB_SERVICE, "Get UsbPort size=%{public}d", ports.size()); +// } + +// /** +// * @tc.name: usb_port_service_test02 +// * @tc.desc: Verify the function +// * @tc.type: FUNC +// * @tc.require: AR00000000 SR00000000 +// */ +// HWTEST_F(UsbPortServiceTest, UsbPortServiceTest02, TestSize.Level1) { +// auto &UsbSrvClient = UsbSrvClient::GetInstance(); +// auto modes = UsbSrvClient.GetSupportedModes(0); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", modes); +// ASSERT_TRUE(modes != 0); +// USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest02 end."); +// } + +// /** +// * @tc.name: usb_port_service_test03 +// * @tc.desc: Verify the function +// * @tc.type: FUNC +// * @tc.require: AR00000000 SR00000000 +// */ +// HWTEST_F(UsbPortServiceTest, UsbPortServiceTest03, TestSize.Level1) { +// auto &UsbSrvClient = UsbSrvClient::GetInstance(); +// auto modes = UsbSrvClient.GetSupportedModes(-1); +// USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", modes); +// ASSERT_TRUE(modes != 0); +// USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest03 end."); +// } + +// /** + * @tc.name: UsbPortServiceTest04 + * @tc.desc: Verify the function + * @tc.type: FUNC + * @tc.require: AR00000000 SR00000000 + */ +HWTEST_F(UsbPortServiceTest, UsbPortServiceTest04, TestSize.Level1) { + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto modes = UsbSrvClient.GetSupportedModes(0xFFFFFFFF); + USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", modes); + ASSERT_TRUE(modes != 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest04 end."); +} + +/** + * @tc.name: usb_port_service_test05 + * @tc.desc: Verify the function + * @tc.type: FUNC + * @tc.require: AR00000000 SR00000000 + */ +HWTEST_F(UsbPortServiceTest, UsbPortServiceTest05, TestSize.Level1) { + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.SetPortRole(1,1,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", 1); + ASSERT_TRUE(ret >= 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest05 end."); +} + +/** + * @tc.name: usb_port_service_test06 + * @tc.desc: Verify the function + * @tc.type: FUNC + * @tc.require: AR00000000 SR00000000 + */ +HWTEST_F(UsbPortServiceTest, UsbPortServiceTest06, TestSize.Level1) { + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.SetPortRole(-1,1,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", 1); + ASSERT_TRUE(ret >= 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest06 end."); +} + +/** + * @tc.name: usb_port_service_test07 + * @tc.desc: Verify the function + * @tc.type: FUNC + * @tc.require: AR00000000 SR00000000 + */ +HWTEST_F(UsbPortServiceTest, UsbPortServiceTest07, TestSize.Level1) { + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.SetPortRole(1,-1,1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", 1); + ASSERT_TRUE(ret >= 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest07 end."); +} + +/** + * @tc.name: usb_port_service_test08 + * @tc.desc: Verify the function + * @tc.type: FUNC + * @tc.require: AR00000000 SR00000000 + */ +HWTEST_F(UsbPortServiceTest, UsbPortServiceTest08, TestSize.Level1) { + auto &UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.SetPortRole(1,1,-1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbServiceTest::status=%{public}d", 1); + ASSERT_TRUE(ret >= 0); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbPortServiceTest08 end."); +} diff --git a/services/native/test/unittest/common/src/usb_request_test.cpp b/services/native/test/unittest/common/src/usb_request_test.cpp new file mode 100644 index 00000000..ee51e416 --- /dev/null +++ b/services/native/test/unittest/common/src/usb_request_test.cpp @@ -0,0 +1,623 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_request_test.h" +#include +#include "usb_pipe.h" +#include "usb_request.h" +#include "usb_srv_client.h" +#include "usb_service.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" +#include "usb_common.h" +#include +#include +#include +#include "iusb_srv.h" +using namespace testing::ext; +using namespace OHOS::USB; +using namespace OHOS; +using namespace std; + +void UsbRequestTest::SetUpTestCase(void) +{ +} + +void UsbRequestTest::TearDownTestCase(void) +{ +} + +void UsbRequestTest::SetUp(void) +{ +} + +void UsbRequestTest::TearDown(void) +{ +} +/** + * @tc.name: Usbrequest001 + * @tc.desc: Test functions of Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,UsbRequest001 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest001 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest001 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest001 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest001 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest001 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest001 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest001 %{public}d ret=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::UsbRequest001 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest001 end."); +} + +/** + * @tc.name: Usbrequest002 + * @tc.desc: Test functions of Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,UsbRequest002 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest002 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest002 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest002 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest002 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest002 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest002 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest002 %{public}d ret=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest002 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest002 end."); +} + +/** + * @tc.name: Usbrequest003 + * @tc.desc: Test functions of Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,UsbRequest003 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest003 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest003 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest003 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest003 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().at(2); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest003 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest003 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest003 %{public}d ret=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest003 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest003 end."); +} + + +/** + * @tc.name: UsbService004 + * @tc.desc: Test functions of Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,UsbRequest004 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest004 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest004 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest004 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest004 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(2); + USBEndpoint point =interface.GetEndpoints().at(2); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest004 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest004 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest004 %{public}d ret=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest004 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest004 end."); +} + +/** + * @tc.name: UsbService005 + * @tc.desc: Test functions of Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest005 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest005 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest005 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest005 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest005 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().at(3); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest005 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest005 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest005 %{public}d ret=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest005 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest005 end."); +} + +/** + * @tc.name: UsbService006 + * @tc.desc: Test functions of Queue(uint8_t *buffer, uint32_t length) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest006 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().front(); + USBEndpoint point =interface.GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + uint32_t len = 255; + uint8_t buffer[255] = {}; + memset(buffer,0,255); + ret = request.Queue(buffer, len); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest006 %{public}d queue=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest006 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest006 end."); +} + +/** + * @tc.name: UsbService007 + * @tc.desc: Test functions of Queue(uint8_t *buffer, uint32_t length) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest007 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().front(); + USBEndpoint point =interface.GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + uint32_t len = 127; + uint8_t buffer[127] = {}; + memset(buffer,0,len); + ret = request.Queue(buffer, len); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest007 %{public}d queue=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest007 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest007 end."); +} + +/** + * @tc.name: UsbService008 + * @tc.desc: Test functions of Queue(uint8_t *buffer, uint32_t length) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest008 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().front(); + USBEndpoint point =interface.GetEndpoints().at(2); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + uint32_t len = 255; + uint8_t buffer[255] = {}; + memset(buffer,0,255); + ret = request.Queue(buffer, len); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest008 %{public}d queue=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest008 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest008 end."); +} + +/** + * @tc.name: UsbService009 + * @tc.desc: Test functions of Queue(uint8_t *buffer, uint32_t length) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest009 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().at(2); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + uint32_t len = 255; + uint8_t buffer[255] = {}; + memset(buffer,0,255); + ret = request.Queue(buffer, len); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:UsbRequest009 %{public}d queue=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest009 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest009 end."); +} + +/** + * @tc.name: UsbService010 + * @tc.desc: Test functions of Queue(uint8_t *buffer, uint32_t length) + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest010 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().at(2); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + uint32_t len = 1; + uint8_t buffer[1] = {0X12}; + memset(buffer,0,len); + ret = request.Queue(buffer, len); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest010 %{public}d queue=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest010 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest010 end."); +} + +/** + * @tc.name: UsbService011 + * @tc.desc: Test functions of free() + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest011 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().front(); + USBEndpoint point =interface.GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + ret = request.Free(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest011 %{public}d free=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest011 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest011 end."); +} + +/** + * @tc.name: UsbService012 + * @tc.desc: Test functions of free() + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest012 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().at(2); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + ret = request.Free(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest012 %{public}d free=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest012 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest012 end."); +} + +/** + * @tc.name: UsbService013 + * @tc.desc: Test functions of abort() + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest013 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().at(1); + USBEndpoint point =interface.GetEndpoints().at(2); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + ret = request.Abort(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest013 %{public}d Abort=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest013 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest013 end."); +} + +/** + * @tc.name: UsbService014 + * @tc.desc: Test functions of abort() + * @tc.type: FUNC + */ +HWTEST_F(UsbRequestTest ,Usbrequest014 ,TestSize.Level1) +{ + vector delist; + auto& UsbSrvClient = UsbSrvClient::GetInstance(); + auto ret = UsbSrvClient.GetDevices(delist); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d ret=%{public}d size:%{public}d", __LINE__, ret, delist.size()); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(!(delist.empty())) << "delist NULL"; + UsbDevice devi = delist.front(); + USBDevicePipe pip; + ret = UsbSrvClient.OpenDevice(devi,pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d ret=%{public}d pip.BusNum=%{public}d pip.DevAddr=%{public}d ", __LINE__, ret, pip.GetBusNum(), pip.GetDevAddr()); + ASSERT_TRUE(ret == ERR_OK); + std::vector configs =devi.GetConfigs(); + USBConfig conf = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d size=%{public}d config:%{public}s ", + __LINE__, configs.size(), conf.ToString().c_str()); + USBConfig config = configs.front(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d ret=%{public}d ", __LINE__, ret); + UsbRequest request; + UsbInterface &interface = config.GetInterfaces().front(); + USBEndpoint point =interface.GetEndpoints().at(1); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d GetDirection=%{public}d ",__LINE__, point.GetDirection()); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d GetInterfaceId=%{public}d ",__LINE__, point.GetInterfaceId()); + ret = request.Initialize(pip,point); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d initialize=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + ret = request.Abort(); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest:Usbrequest014 %{public}d abort=%{public}d ",__LINE__, ret ); + EXPECT_TRUE(ret == ERR_OK); + bool close = UsbSrvClient.Close(pip); + USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::Usbrequest014 %{public}d close=%{public}d",__LINE__, close); + EXPECT_TRUE(close); + USB_HILOGD(MODULE_USB_SERVICE, "UsbService::Usbrequest014 end."); +} \ No newline at end of file diff --git a/services/native/test/unittest/common/src/usb_service_test.cpp b/services/native/test/unittest/common/src/usb_service_test.cpp new file mode 100644 index 00000000..48d825b8 --- /dev/null +++ b/services/native/test/unittest/common/src/usb_service_test.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_service_test.h" +#include +#include "usb_srv_client.h" +#include "usb_service.h" +#include "iservice_registry.h" +#include "if_system_ability_manager.h" +#include "system_ability_definition.h" +#include "ipc_skeleton.h" +#include "string_ex.h" +#include + +using namespace testing::ext; +using namespace OHOS::USB; +using namespace OHOS; +using namespace std; + +void UsbServiceTest::SetUpTestCase(void) +{ +} + +void UsbServiceTest::TearDownTestCase(void) +{ +} + +void UsbServiceTest::SetUp(void) +{ +} + +void UsbServiceTest::TearDown(void) +{ +} + +/** + * @tc.name: UsbService001 + * @tc.desc: Test functions to get status of Present + * @tc.type: FUNC + */ +// HWTEST_F (UsbServiceTest, UsbService001, TestSize.Level1) +// { +// USB_HILOGI(MODULE_USB_SERVICE, "Usb ServiceTest::present=%{public}d", 888) +// USB_HILOGD(MODULE_USB_SERVICE, "UsbService::UsbService001 end."); +// } + + diff --git a/services/zidl/include/usb_server_proxy.h b/services/zidl/include/usb_server_proxy.h new file mode 100644 index 00000000..e1f4acf5 --- /dev/null +++ b/services/zidl/include/usb_server_proxy.h @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBMGR_USB_SERVER_PROXY_H +#define USBMGR_USB_SERVER_PROXY_H + +#include "iremote_proxy.h" +#include "iusb_srv.h" +#include "nocopyable.h" +#include "usb_device.h" +#include +#include + +namespace OHOS { +namespace USB { + +class UsbServerProxy : public IRemoteProxy { + public: + explicit UsbServerProxy(const sptr &impl) + : IRemoteProxy(impl) {} + ~UsbServerProxy() = default; + DISALLOW_COPY_AND_MOVE(UsbServerProxy); + + virtual int32_t GetDevice(uint8_t busNum, uint8_t devAddr, UsbDevice &devInfo) override; + virtual int32_t GetDevices(std::vector &deviceList) override; + virtual int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override; + virtual int32_t HasRight(std::string deviceName) override; + virtual int32_t RequestRight(std::string deviceName) override; + virtual int32_t GetCurrentFunctions() override; + virtual bool SetCurrentFunctions(int32_t funcs) override; + virtual int32_t UsbFunctionsFromString(std::string funcs) override; + virtual std::string UsbFunctionsToString(int32_t funcs) override; + virtual int32_t GetPorts(std::vector &result) override; + virtual int32_t GetSupportedModes(int32_t portId, int32_t &result) override; + virtual int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override; + + virtual int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override; + virtual int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override; + virtual int32_t BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout) override; + virtual int32_t BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpoint, const uint8_t *buffer, uint32_t length, int32_t timeout) override; + virtual int32_t ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout) override; + virtual int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override; + virtual int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) override; + virtual int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) override; + virtual int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) override; + virtual int32_t RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointIndex, const void *clientObj, uint32_t objLength, const uint8_t *buffer, uint32_t length) override; + virtual int32_t RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, void *clientData, uint32_t &clientLength, uint8_t *buffer, uint32_t &length) override; + virtual int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) override; + virtual int32_t Close(uint8_t busNum, uint8_t devAddr) override; + +private: + static inline BrokerDelegator delegator_; + void ParseUsbPort(MessageParcel &reply, std::vector &result); + int32_t SetDeviceMessage(MessageParcel &data, uint8_t busNum, uint8_t devAddr); + int32_t SetBufferMessage(MessageParcel &data, const void *buffer, uint32_t length); + int32_t GetBufferMessage(MessageParcel &data, void *buffer, uint32_t &length); + int32_t GetDeviceListMessageParcel(MessageParcel &data, std::vector &deviceList); + int32_t GetDeviceMessageParcel(MessageParcel &data, UsbDevice &devInfo); + int32_t GetDeviceConfigsMessageParcel(MessageParcel &data, std::vector &configs); + int32_t GetDeviceInterfacesMessageParcel(MessageParcel &data, std::vector &interfaces); + int32_t GetDeviceEndpointsMessageParcel(MessageParcel &data, std::vector &eps); +}; + +} // namespace USB +} // namespace OHOS + +#endif // USBMGR_USB_SERVER_PROXY_H diff --git a/services/zidl/include/usb_server_stub.h b/services/zidl/include/usb_server_stub.h new file mode 100644 index 00000000..512eb1db --- /dev/null +++ b/services/zidl/include/usb_server_stub.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USBMGR_USB_SERVER_STUB_H +#define USBMGR_USB_SERVER_STUB_H + +#include +#include "nocopyable.h" +#include "iremote_stub.h" +#include "iusb_srv.h" +#include "usb_device.h" + +namespace OHOS { +namespace USB { + +class UsbServerStub : public IRemoteStub { +public: + DISALLOW_COPY_AND_MOVE(UsbServerStub); + + UsbServerStub() = default; + virtual ~UsbServerStub() = default; + + virtual int32_t OnFoundDevice(FoundDevice &info) = 0; + + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) override; + +private: + const FoundDevice * ParserOpenDeviceInfo(MessageParcel &data, MessageParcel &reply, + MessageOption &option); + + int32_t DoGetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoSetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoUsbFunctionsFromString(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoUsbFunctionsToString(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + int32_t DoOpenDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoHasRight(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoRequestRight(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoGetPorts(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoGetSupportedModes(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoSetPortRole(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoClaimInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoReleaseInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoBulkTransferRead(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoBulkTransferWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoControlTransfer(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoSetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoGetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoSetInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoGetRawDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoRequestQueue(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoRequestWait(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoRequestCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + int32_t DoClose(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoGetDevices(MessageParcel &data, MessageParcel &reply, MessageOption &option); + int32_t DoGetDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option); + + void WriteUsbPort(MessageParcel &reply, const UsbPort *port); + + int32_t GetDeviceMessage(MessageParcel &data, uint8_t &busNum, uint8_t &devAddr); + int32_t SetBufferMessage(MessageParcel &data, const void *buffer, uint32_t length); + int32_t GetBufferMessage(MessageParcel &data, void *buffer, uint32_t &length); + + int32_t SetDeviceListMessageParcel(std::vector &deviceList, MessageParcel &data); + int32_t SetDeviceMessageParcel(UsbDevice &devInfo, MessageParcel &data); + int32_t SetDeviceConfigsMessageParcel(std::vector &configs, MessageParcel &data); + int32_t SetDeviceInterfacesMessageParcel(std::vector &interfaces, MessageParcel &data); + int32_t SetDeviceEndpointsMessageParcel(std::vector &eps, MessageParcel &data); +}; + +} // namespace USB +} // namespace OHOS + +#endif // USBMGR_USB_SERVER_STUB_H diff --git a/services/zidl/iusb_server.zidl b/services/zidl/iusb_server.zidl new file mode 100644 index 00000000..c0dc2568 --- /dev/null +++ b/services/zidl/iusb_server.zidl @@ -0,0 +1,19 @@ +/* + * Copyright 2021 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. + */ + +interface OHOS.Usb.IUsbServer { + /* the function about UsbService */ +} diff --git a/services/zidl/src/usb_srv_proxy.cpp b/services/zidl/src/usb_srv_proxy.cpp new file mode 100644 index 00000000..534ad6b4 --- /dev/null +++ b/services/zidl/src/usb_srv_proxy.cpp @@ -0,0 +1,840 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_server_proxy.h" +#include +#include +#include "securec.h" +#include "message_parcel.h" +#include "ipc_types.h" +#include "usb_common.h" +#include "usb_request.h" + +namespace OHOS { +namespace USB { + +void PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length) +{ + std::ostringstream oss; + if(title == NULL || buffer == nullptr || length == 0) { + return; + } + oss.str(""); + oss << title << " < binary data [" << length << "bytes] > :" ; + for(uint32_t i = 0; i < length; ++i) { + oss << " " << std::hex << (int)buffer[i]; + } + oss << " -> " << buffer << std::endl; + USB_HILOGD(MODULE_USBD, "%{public}s", oss.str().c_str()); +} + +int32_t UsbServerProxy::SetDeviceMessage(MessageParcel &data, uint8_t busNum, uint8_t devAddr) +{ + WRITE_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerProxy::SetBufferMessage(MessageParcel &data, const void *buffer, uint32_t length) +{ + if(!data.WriteUint32(length)) { + return ERR_INVALID_VALUE; + } + + if((length > 0) && !data.WriteBuffer(buffer, length)) { + return ERR_INVALID_VALUE; + } + return ERR_OK; +} + +int32_t UsbServerProxy::GetBufferMessage(MessageParcel &data, void *buffer, uint32_t &length) +{ + uint32_t dataSize; + if(!data.ReadUint32(dataSize)) { + return ERR_INVALID_VALUE; + } + if(dataSize == 0) { + length = 0; + return ERR_OK; + } + const uint8_t *readData = data.ReadBuffer(dataSize); + if(readData == nullptr) { + return ERR_INVALID_VALUE; + } + memcpy_s(buffer, length, readData, dataSize); + length = dataSize; + USB_HILOGW(MODULE_USBD, "%{public}s Get buffer message. length = %{public}d", __func__, length); + return ERR_OK; +} + +int32_t UsbServerProxy::GetDevice(uint8_t busNum, uint8_t devAddr, UsbDevice &devInfo) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_INVALID_VALUE; + } + + SetDeviceMessage(data, busNum, devAddr); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_GET_DEVICE), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, ret); + return ret; + } + GetDeviceMessageParcel(reply, devInfo); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s UsbServerProxy devInfo=%{public}s", __func__, devInfo.ToString().c_str()); + return ret; +} + +int32_t UsbServerProxy::GetDevices(std::vector &deviceList) +{ + int32_t ret; + sptr remote = Remote(); + if (remote == nullptr) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s remote is failed", __func__); + return ERR_INVALID_VALUE; + } + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_INVALID_VALUE; + } + + ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_GET_DEVICES), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s failed code: %{public}d", __func__, ret); + return ret; + } + ret = GetDeviceListMessageParcel(reply, deviceList); + return ret; +} + +int32_t UsbServerProxy::GetDeviceListMessageParcel(MessageParcel &data, std::vector &deviceList) +{ + int32_t count; + data.ReadInt32(count); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s device size : %{public}d", __func__, count); + + for(int32_t i = 0; i < count; ++i) { + UsbDevice devInfo; + GetDeviceMessageParcel(data, devInfo); + deviceList.push_back(devInfo); + } + return ERR_OK; +} + +int32_t UsbServerProxy::GetDeviceMessageParcel(MessageParcel &data, UsbDevice &devInfo) +{ + int32_t tmp; + uint8_t tui8; + uint16_t tui16; + std::string tstr; + data.ReadInt32(tmp); + devInfo.SetBusNum(tmp); + data.ReadInt32(tmp); + devInfo.SetDevAddr(tmp); + + data.ReadInt32(tmp); + devInfo.SetVendorId(tmp); + data.ReadInt32(tmp); + devInfo.SetProductId(tmp); + data.ReadInt32(tmp); + devInfo.SetClass(tmp); + data.ReadInt32(tmp); + devInfo.SetSubclass(tmp); + data.ReadInt32(tmp); + devInfo.SetProtocol(tmp); + data.ReadUint8(tui8); + devInfo.SetiManufacturer(tui8); + data.ReadUint8(tui8); + devInfo.SetiProduct(tui8); + data.ReadUint8(tui8); + devInfo.SetiSerialNumber(tui8); + data.ReadUint8(tui8); + devInfo.SetbMaxPacketSize0(tui8); + data.ReadUint16(tui16); + devInfo.SetbcdUSB(tui16); + data.ReadUint16(tui16); + devInfo.SetbcdDevice(tui16); + data.ReadString(tstr); + devInfo.SetName(tstr); + data.ReadString(tstr); + devInfo.SetManufacturerName(tstr); + data.ReadString(tstr); + devInfo.SetProductName(tstr); + data.ReadString(tstr); + devInfo.SetVersion(tstr); + data.ReadString(tstr); + devInfo.SetmSerial(tstr); + + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:%{public}d devName:%{public}s Bus:%{public}d dev:%{public}d ", + __func__, __LINE__, devInfo.GetName().c_str(), devInfo.GetBusNum(), devInfo.GetDevAddr()); + std::vector configs; + GetDeviceConfigsMessageParcel(data, configs); + devInfo.SetConfigs(configs); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s ToString : %{public}s", __func__, devInfo.ToString().c_str()); + return ERR_OK; +} + +int32_t UsbServerProxy::GetDeviceConfigsMessageParcel(MessageParcel &data, std::vector &configs) +{ + int32_t tmp, configCount; + uint8_t tui8; + std::string tstr; + data.ReadInt32(tmp); + configCount = tmp; + for(int i = 0; i < configCount; ++i) { + USBConfig config; + data.ReadInt32(tmp); + config.SetId(tmp); + data.ReadInt32(tmp); + config.SetAttribute(tmp); + data.ReadInt32(tmp); + config.SetMaxPower(tmp); + + data.ReadUint8(tui8); + config.SetiConfiguration(tui8); + data.ReadString(tstr); + config.SetName(tstr); + + std::vector interfaces; + GetDeviceInterfacesMessageParcel(data, interfaces); + config.SetInterfaces(interfaces); + configs.push_back(config); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s devInfo=%{public}s", __func__, config.ToString().c_str()); + } + + return ERR_OK; +} + +int32_t UsbServerProxy::GetDeviceInterfacesMessageParcel(MessageParcel &data, std::vector &interfaces) +{ + int32_t tmp, interfaceCount; + uint8_t tui8; + std::string tstr; + data.ReadInt32(tmp); + interfaceCount = tmp; + for(int i = 0; i < interfaceCount; ++i) { + UsbInterface interface; + data.ReadInt32(tmp); + interface.SetId(tmp); + data.ReadInt32(tmp); + interface.SetClass(tmp); + data.ReadInt32(tmp); + interface.SetSubClass(tmp); + data.ReadInt32(tmp); + interface.SetAlternateSetting(tmp); + data.ReadInt32(tmp); + interface.SetProtocol(tmp); + + data.ReadUint8(tui8); + interface.SetiInterface(tui8); + data.ReadString(tstr); + interface.SetName(tstr); + + std::vector eps; + GetDeviceEndpointsMessageParcel(data, eps); + for(size_t j = 0; j < eps.size(); ++j) { + eps[j].SetInterfaceId(interface.GetId()); + } + interface.SetEndpoints(eps); + interfaces.push_back(interface); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s devInfo=%{public}s", __func__, interface.ToString().c_str()); + } + return ERR_OK; +} + +int32_t UsbServerProxy::GetDeviceEndpointsMessageParcel(MessageParcel &data, std::vector &eps) +{ + int32_t tmp, epCount; + data.ReadInt32(tmp); + epCount = tmp; + for(int i = 0; i < epCount; ++i) { + USBEndpoint ep; + data.ReadInt32(tmp); + ep.SetAddr(tmp); + data.ReadInt32(tmp); + ep.SetAttr(tmp); + data.ReadInt32(tmp); + ep.SetInterval(tmp); + data.ReadInt32(tmp); + ep.SetMaxPacketSize(tmp); + eps.push_back(ep); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s devInfo=%{public}s", __func__, ep.ToString().c_str()); + } + return ERR_OK; +} + +int32_t UsbServerProxy::OpenDevice(uint8_t busNum, uint8_t devAddr) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", + __func__); + return ERR_ENOUGH_DATA; + } + + SetDeviceMessage(data, busNum, devAddr); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_OPEN_DEVICE), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %{public}d", __func__, ret); + return ret; + } + //READ_PARCEL_WITH_RET(reply, Int32, fd, E_READ_PARCEL_ERROR); + return ret; +} + +int32_t UsbServerProxy::HasRight(std::string deviceName) { + MessageParcel data; + MessageParcel reply; + MessageOption option; + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_USB_INT_VALUE; + } + WRITE_PARCEL_WITH_RET(data, String, deviceName, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_HAS_RIGHT), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return INVALID_USB_INT_VALUE; + } + int32_t result = 0; + READ_PARCEL_WITH_RET(reply, Int32, result, E_READ_PARCEL_ERROR); + return result; +} + +int32_t UsbServerProxy::RequestRight(std::string deviceName) { + MessageParcel data; + MessageParcel reply; + MessageOption option; + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_USB_INT_VALUE; + } + WRITE_PARCEL_WITH_RET(data, String, deviceName, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_REQUEST_RIGHT), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return INVALID_USB_INT_VALUE; + } + int32_t result = 0; + READ_PARCEL_WITH_RET(reply, Int32, result, E_READ_PARCEL_ERROR); + return result; +} + +int32_t UsbServerProxy::GetCurrentFunctions() { + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, INVALID_USB_INT_VALUE); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_USB_INT_VALUE; + } + + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_GET_CURRENT_FUNCTIONS), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return INVALID_USB_INT_VALUE; + } + + int32_t result = 0; + READ_PARCEL_WITH_RET(reply, Int32, result, INVALID_USB_INT_VALUE); + return result; +} + +bool UsbServerProxy::SetCurrentFunctions(int32_t funcs) { + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, false); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return false; + } + + data.WriteInt32(funcs); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_SET_CURRENT_FUNCTIONS), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return false; + } + return true; +} + +int32_t UsbServerProxy::UsbFunctionsFromString(std::string funcs) { + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, INVALID_USB_INT_VALUE); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_USB_INT_VALUE; + } + + data.WriteString(funcs); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_USB_FUNCTIONS_FROM_STRING), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return INVALID_USB_INT_VALUE; + } + + int32_t result = 0; + READ_PARCEL_WITH_RET(reply, Int32, result, INVALID_USB_INT_VALUE); + return result; +} + +std::string UsbServerProxy::UsbFunctionsToString(int32_t funcs) { + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, INVALID_STRING_VALUE); + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_STRING_VALUE; + } + + data.WriteInt32(funcs); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_USB_FUNCTIONS_TO_STRING), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return INVALID_STRING_VALUE; + } + + std::string result; + READ_PARCEL_WITH_RET(reply, String, result, result); + return result; +} + +int32_t UsbServerProxy::GetPorts(std::vector &result) { + MessageParcel data; + MessageParcel reply; + MessageOption option; + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_USB_INT_VALUE; + } + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_GET_PORTS), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return INVALID_USB_INT_VALUE; + } + ret = reply.ReadInt32(); + USB_HILOGI(MODULE_USB_INNERKIT, "GetPorts ret %{public}d", ret); + if (ret != 0) { + return ret; + } + uint32_t size = reply.ReadInt32(); + USB_HILOGI(MODULE_USB_INNERKIT, "GetPorts size %{public}d", size); + for (uint32_t i = 0; i < size; ++i) { + USB_HILOGI(MODULE_USB_INNERKIT, "ParseUsbPort : %{public}d", i); + ParseUsbPort(reply, result); + } + return ret; +} + +void UsbServerProxy::ParseUsbPort(MessageParcel &reply, std::vector &result) { + UsbPort *port = new UsbPort(); + UsbPortStatus *status = new UsbPortStatus(); + port->id = reply.ReadInt32(); + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::port->id %{public}d", port->id); + port->supportedModes = reply.ReadInt32(); + port->usbPortStatus = status; + status->currentMode = reply.ReadInt32(); + status->currentPowerRole = reply.ReadInt32(); + status->currentDataRole = reply.ReadInt32(); + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::port->usbPortStatus->currentMode %{public}d", + port->usbPortStatus->currentMode); + result.push_back(port); + return; +} + +int32_t UsbServerProxy::GetSupportedModes(int32_t portId, int32_t &result) { + MessageParcel data; + MessageParcel reply; + MessageOption option; + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_USB_INT_VALUE; + } + data.WriteInt32(portId); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_GET_SUPPORTED_MODES), data, reply, option); + if (ret) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return ret; + } + ret = reply.ReadInt32(); + result = reply.ReadInt32(); + return ret; +} + +int32_t UsbServerProxy::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) { + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return INVALID_USB_INT_VALUE; + } + data.WriteInt32(portId); + data.WriteInt32(powerRole); + data.WriteInt32(dataRole); + int32_t ret = Remote()->SendRequest(static_cast(IUsbSrv::USB_FUN_SET_PORT_ROLE), data, reply, option); + if (ret) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return ret; + } + int32_t result = reply.ReadInt32(); + return result; +} + +int32_t UsbServerProxy::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interface, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_CLAIM_INTERFACE), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %{public}d", __func__, + ret); + return ret; + } + return ret; +} +int32_t UsbServerProxy::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interface, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_RELEASE_INTERFACE), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return ret; + } + // int32_t ret = ERR_OK; + //READ_PARCEL_WITH_RET(reply, Int32, ret, E_READ_PARCEL_ERROR); + return ret; +} +int32_t UsbServerProxy::BulkTransferRead(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t endpoint, uint8_t *buffer, uint32_t &length, int32_t timeout) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interface, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, endpoint, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint32, length, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_BULK_TRANSFER_READ), data, reply, option); + if (ret != ERR_OK) { + USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %d", __func__, + ret); + return 0; + } + GetBufferMessage(reply, buffer, length); + USB_HILOGW(MODULE_USBD, "%{public}s Set buffer message. length = %{public}d", __func__, length); + return ret; +} +int32_t UsbServerProxy::BulkTransferWrite(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t endpoint, const uint8_t *buffer, uint32_t length, int32_t timeout) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + data.WriteUint8(interface); + data.WriteUint8(endpoint); + data.WriteUint32(timeout); + SetBufferMessage(data, buffer, length); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_BULK_TRANSFER_WRITE), + data, reply, option); + return ret; +} + +int32_t UsbServerProxy::ControlTransfer(uint8_t busNum, uint8_t devAddr, int32_t requestType, int32_t request, int32_t value, int32_t index, uint8_t *buffer, uint32_t &length, int32_t timeout) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Int32, requestType, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, request, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, value, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, index, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint32, length, E_WRITE_PARCEL_ERROR); + bool bWrite = ((requestType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT); + if(bWrite) { + data.WriteBuffer(buffer, length); + } + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_CONTROL_TRANSFER), + data, reply, option); + if(ret != ERR_OK) { + return ret; + } + if (!bWrite) { + GetBufferMessage(reply, buffer, length); + PrintBuffer("UsbServerProxy::ControlTransfer", buffer, length); + USB_HILOGW(MODULE_USBD, "%{public}s Get buffer message. length = %{public}d", __func__, length); + } + return ERR_OK; +} +int32_t UsbServerProxy::SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, configIndex, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_SET_ACTIVE_CONFIG), + data, reply, option); + return ret; +} +int32_t UsbServerProxy::GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_GET_ACTIVE_CONFIG), data, reply, option); + if(ret != ERR_OK) { + return ret; + } + READ_PARCEL_WITH_RET(reply, Uint8, configIndex, E_WRITE_PARCEL_ERROR); + return ret; +} +int32_t UsbServerProxy::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, altIndex, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_SET_INTERFACE), + data, reply, option); + return ret; +} +int32_t UsbServerProxy::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, uint8_t *buffer, uint32_t &length) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_GET_DESCRIPTOR), data, reply, option); + if(ret == ERR_OK) { + GetBufferMessage(reply, buffer, length); + } + return ret; +} + +int32_t UsbServerProxy::RequestQueue(uint8_t busNum, uint8_t devAddr, uint8_t ifId, uint8_t endpointId, const void *clientObj, uint32_t clientLength, const uint8_t *buffer, uint32_t length) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s get descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, ifId, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, endpointId, E_WRITE_PARCEL_ERROR); + SetBufferMessage(data, clientObj, clientLength); + SetBufferMessage(data, buffer, length); + //PrintBuffer("UsbServerProxy::RequestQueue buffer", buffer, length); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_REQUEST_QUEUE), data, reply, option); + if(ret != ERR_OK) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s queue failed!", __func__); + return ret; + } + return ret; +} + +int32_t UsbServerProxy::RequestWait(uint8_t busNum, uint8_t devAddr, int32_t timeout, void *clientData, uint32_t &clientLength, uint8_t *buffer, uint32_t &length) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s get descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_REQUEST_WAIT), data, reply, option); + if(ret != ERR_OK) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s:%{public}d queue failed! ret:%{public}d", __func__, __LINE__, ret); + return ret; + } + + GetBufferMessage(reply, clientData, clientLength); + GetBufferMessage(reply, buffer, length); + //PrintBuffer("UsbServerProxy::RequestWait clientData", clientData, clientLength); + PrintBuffer("UsbServerProxy::RequestWait buffer", buffer, length); + return ret; +} + +int32_t UsbServerProxy::RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) +{ + int32_t ret; + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s get descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + + SetDeviceMessage(data, busNum, devAddr); + WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, E_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint8, endpointId, E_WRITE_PARCEL_ERROR); + ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_REQUEST_CANCEL), data, reply, option); + if(ret != ERR_OK) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s request cancel failed!", __func__); + } + + return ret; +} + +int32_t UsbServerProxy::Close(uint8_t busNum, uint8_t devAddr) +{ + sptr remote = Remote(); + RETURN_IF_WITH_RET(remote == nullptr, E_INNER_ERR); + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s get descriptor failed!", __func__); + return ERR_ENOUGH_DATA; + } + + SetDeviceMessage(data, busNum, devAddr); + int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_CLOSE_DEVICE), data, reply, option); + if(ret != ERR_OK) { + USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s queue failed!", __func__); + return ret; + } + + return ERR_OK; +} + +} // namespace USB +} // namespace OHOS diff --git a/services/zidl/src/usb_srv_stub.cpp b/services/zidl/src/usb_srv_stub.cpp new file mode 100644 index 00000000..f3e68a2e --- /dev/null +++ b/services/zidl/src/usb_srv_stub.cpp @@ -0,0 +1,656 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_server_stub.h" +#include +#include +#include +#include "securec.h" +#include "message_parcel.h" +#include "usb_common.h" +#include "usb_server_proxy.h" + + +namespace OHOS { +namespace USB { + +const uint32_t MAX_REQUEST_BUFFER_SIZE = 1024; + +void PrintBuffer(const char *title, const uint8_t *buffer, uint32_t length) +{ + std::ostringstream oss; + if(title == NULL || buffer == nullptr || length == 0) { + return; + } + oss.str(""); + oss << title << " << BINARY DATA [" << length << "bytes] >> :" ; + for(uint32_t i = 0; i < length; ++i) { + oss << " " << std::hex << (int)buffer[i]; + } + oss << " --> " << buffer << std::endl; + USB_HILOGD(MODULE_USB_SERVICE, "%{public}s", oss.str().c_str()); +} + +int32_t UsbServerStub::GetDeviceMessage(MessageParcel &data, uint8_t &busNum, uint8_t &devAddr) +{ + if(!data.ReadUint8(busNum)) { + return ERR_INVALID_VALUE; + } + if(!data.ReadUint8(devAddr)) { + return ERR_INVALID_VALUE; + } + return ERR_OK; +} + +int32_t UsbServerStub::SetBufferMessage(MessageParcel &data, const void *buffer, uint32_t length) +{ + if(!data.WriteUint32(length)) { + return ERR_INVALID_VALUE; + } + + if((length > 0) && !data.WriteBuffer(buffer, length)) { + return ERR_INVALID_VALUE; + } + return ERR_OK; +} + +int32_t UsbServerStub::GetBufferMessage(MessageParcel &data, void *buffer, uint32_t &length) +{ + uint32_t dataSize; + if(!data.ReadUint32(dataSize)) { + return ERR_INVALID_VALUE; + } + if(dataSize == 0) { + length = 0; + return ERR_OK; + } + const uint8_t *readData = data.ReadBuffer(dataSize); + if(readData == nullptr) { + return ERR_INVALID_VALUE; + } + memcpy_s(buffer, length, readData, dataSize); + length = dataSize; + return ERR_OK; +} +const FoundDevice *UsbServerStub::ParserOpenDeviceInfo(MessageParcel &data, MessageParcel &reply, + MessageOption &option) { + + return nullptr; +} + +int UsbServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + USB_HILOGD(MODULE_USB_SERVICE, "UsbServerStub::OnRemoteRequest, cmd = %{public}d, flags = %{public}d", + code, option.GetFlags()); + std::u16string descriptor = UsbServerStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + USB_HILOGE(MODULE_SERVICE, "UsbServerStub::OnRemoteRequest failed, descriptor is not matched!"); + return E_GET_USB_SERVICE_FAILED; + } + + switch (code) { + case static_cast(IUsbSrv::USB_FUN_OPEN_DEVICE): { + return DoOpenDevice(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_HAS_RIGHT): { + return DoHasRight(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_REQUEST_RIGHT): { + return DoRequestRight(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_GET_PORTS): { + return DoGetPorts(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_GET_SUPPORTED_MODES): { + return DoGetSupportedModes(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_SET_PORT_ROLE): { + return DoSetPortRole(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_CLAIM_INTERFACE): { + return DoClaimInterface(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_RELEASE_INTERFACE): { + return DoReleaseInterface(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_BULK_TRANSFER_READ): { + return DoBulkTransferRead(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_BULK_TRANSFER_WRITE): { + return DoBulkTransferWrite(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_CONTROL_TRANSFER): { + return DoControlTransfer(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_GET_DEVICE): { + return DoGetDevice(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_GET_DEVICES): { + return DoGetDevices(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_GET_CURRENT_FUNCTIONS): { + return DoGetCurrentFunctions(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_SET_CURRENT_FUNCTIONS): { + return DoSetCurrentFunctions(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_USB_FUNCTIONS_FROM_STRING): { + return DoUsbFunctionsFromString(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_USB_FUNCTIONS_TO_STRING): { + return DoUsbFunctionsToString(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_CLOSE_DEVICE): { + return DoClose(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_REQUEST_QUEUE): { + return DoRequestQueue(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_REQUEST_WAIT): { + return DoRequestWait(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_SET_INTERFACE): { + return DoSetInterface(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_SET_ACTIVE_CONFIG): { + return DoSetActiveConfig(data, reply, option); + } + case static_cast(IUsbSrv::USB_FUN_REQUEST_CANCEL): { + return DoRequestCancel(data, reply, option); + } + + /* + case static_cast(IUsbServer::USB_NOTICE_ADD_DEVICE): { + const OpenDeviceInfo *info = ParserOpenDeviceInfo(data, + reply, option); return NoticeAddDevice(info); + } + break; + */ + default: { + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + } + + return ERR_OK; +} + + +int32_t UsbServerStub::DoGetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t funcs = GetCurrentFunctions(); + USB_HILOGI(MODULE_SERVICE, "DoGetCurrentFunctions %{public}d ", funcs); + WRITE_PARCEL_WITH_RET(reply, Int32, funcs, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerStub::DoSetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t funcs; + data.ReadInt32(funcs); + + bool ret = SetCurrentFunctions(funcs); + WRITE_PARCEL_WITH_RET(reply, Bool, ret, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerStub::DoUsbFunctionsFromString(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + std::string funcs; + data.ReadString(funcs); + + int32_t ret = UsbFunctionsFromString(funcs); + WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerStub::DoUsbFunctionsToString(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t funcs; + data.ReadInt32(funcs); + + std::string ret = UsbFunctionsToString(funcs); + WRITE_PARCEL_WITH_RET(reply, String, ret, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerStub::DoOpenDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_READ_PARCEL_ERROR); + //int32_t fd; + int32_t ret = OpenDevice(busNum, devAddr); + if (ret != ERR_OK) { + return ret; + } + + //WRITE_PARCEL_WITH_RET(reply, Int32, fd, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerStub::DoHasRight(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + std::string deviceName = ""; + READ_PARCEL_WITH_RET(data, String, deviceName, E_READ_PARCEL_ERROR); + int32_t ret = HasRight(deviceName); + WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerStub::DoRequestRight(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + std::string deviceName = ""; + READ_PARCEL_WITH_RET(data, String, deviceName, E_READ_PARCEL_ERROR); + int32_t ret = RequestRight(deviceName); + WRITE_PARCEL_WITH_RET(reply, Int32, ret, E_WRITE_PARCEL_ERROR); + return ERR_OK; +} + +int32_t UsbServerStub::DoGetPorts(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + std::vector result; + int ret = GetPorts(result); + USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts ret %{public}d ", ret); + reply.WriteInt32(ret); + if (ret == 0) { + unsigned int size = result.size(); + USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts size %{public}d ", size); + reply.WriteInt32(size); + for (unsigned int i = 0; i < size; ++i) { + WriteUsbPort(reply, result[i]); + } + } + return ret; +} + +void UsbServerStub::WriteUsbPort(MessageParcel &reply, const UsbPort *port) +{ + reply.WriteInt32(port->id); + reply.WriteInt32(port->supportedModes); + reply.WriteInt32(port->usbPortStatus->currentMode); + reply.WriteInt32(port->usbPortStatus->currentPowerRole); + reply.WriteInt32(port->usbPortStatus->currentDataRole); + USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts supportedModes %{public}d ", port->supportedModes); + return; +} + +int32_t UsbServerStub::DoGetSupportedModes(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t result = 0; + int32_t portId = 0; + portId = data.ReadInt32(); + int32_t ret = GetSupportedModes(portId, result); + reply.WriteInt32(ret); + reply.WriteInt32(result); + return ret; +} + +int32_t UsbServerStub::DoSetPortRole(MessageParcel &data, MessageParcel &reply, MessageOption &option) { + int32_t portId = 0; + int32_t powerRole = 0; + int32_t dataRole = 0; + portId = data.ReadInt32(); + powerRole = data.ReadInt32(); + dataRole = data.ReadInt32(); + int32_t ret = SetPortRole(portId, powerRole, dataRole); + reply.WriteInt32(ret); + return ret; +} + +int32_t UsbServerStub::DoClaimInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + uint8_t interface; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, interface, E_READ_PARCEL_ERROR); + return ClaimInterface(busNum, devAddr, interface); +} +int32_t UsbServerStub::DoReleaseInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + uint8_t interface; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_READ_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, interface, E_READ_PARCEL_ERROR); + return ReleaseInterface(busNum, devAddr, interface); +} + +int32_t UsbServerStub::DoBulkTransferRead(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0, interface, endpoint; + int32_t timeout = 0; + uint32_t length; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, interface, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, endpoint, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, length, E_WRITE_PARCEL_ERROR); + uint8_t *buffer = new uint8_t[length]; + if(buffer == nullptr) { + return ERR_NO_MEMORY; + } + int32_t ret = BulkTransferRead(busNum, devAddr, interface, endpoint, buffer, length, timeout); + if(ret != 0) { + delete [] buffer; + return ret; + } + SetBufferMessage(reply, buffer, length); + USB_HILOGW(MODULE_USBD, "%{public}s Set buffer message. length = %{public}d", __func__, length); + delete [] buffer; + return 0; +} + +int32_t UsbServerStub::DoBulkTransferWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0, interface, endpoint; + int32_t timeout = 0; + uint32_t length = 0; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, interface, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, endpoint, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, length, E_WRITE_PARCEL_ERROR); + const uint8_t *buffer = data.ReadBuffer(length); + if(buffer == nullptr) { + return ERR_NO_MEMORY; + } + int32_t ret = BulkTransferWrite(busNum, devAddr, interface, endpoint, buffer, length, timeout); + return ret; +} + +int32_t UsbServerStub::DoControlTransfer(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + int32_t requestType, request, value, index, timeout; + uint32_t length; + uint8_t *buffer = nullptr; + + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, requestType, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, request, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, value, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, index, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, length, E_WRITE_PARCEL_ERROR); + + buffer = new uint8_t[length]; + if(buffer == nullptr) { + return ERR_NO_MEMORY; + } + // 写数据 + bool bWrite = ((requestType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT); + if (bWrite) { + const uint8_t *dataBuffer = data.ReadBuffer(length); + memcpy_s(buffer, length, dataBuffer, length); + } + int32_t ret = ControlTransfer(busNum, devAddr, requestType, request, value, index, buffer, length, timeout); + if (ret != ERR_OK) { + delete[] buffer; + return ret; + } + // 读数据 + if (!bWrite && buffer != nullptr) { + SetBufferMessage(reply, buffer, length); + USB_HILOGW(MODULE_USBD, "%{public}s Set buffer message. length = %{public}d", __func__, length); + PrintBuffer("UsbServerStub::DoControlTransfer", buffer, length); + } + + delete[] buffer; + return ERR_OK; +} + +int32_t UsbServerStub::DoSetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option) { + uint8_t busNum = 0, devAddr = 0, config = 0; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, config, E_WRITE_PARCEL_ERROR); + return SetActiveConfig(busNum, devAddr, config); +} + +int32_t UsbServerStub::DoGetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option) { + uint8_t busNum = 0, devAddr = 0, config = 0; + int32_t ret; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + ret = GetActiveConfig(busNum, devAddr, config); + if (ret == ERR_OK) { + reply.WriteUint8(config); + } + return ret; +} + +int32_t UsbServerStub::DoSetInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0, interfaceId, altIndex = 0; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, interfaceId, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, altIndex, E_WRITE_PARCEL_ERROR); + return SetInterface(busNum, devAddr, interfaceId, altIndex); +} + +int32_t UsbServerStub::DoGetRawDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option) { + int32_t ret = ERR_OK; + uint8_t busNum = 0, devAddr = 0; + uint32_t length = 0; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint32, length, E_WRITE_PARCEL_ERROR); + uint8_t *buffer = new uint8_t[length]; + ret = GetRawDescriptor(busNum, devAddr, buffer, length); + if (ret == ERR_OK) { + SetBufferMessage(reply, buffer, length); + } + delete [] buffer; + return ret; +} + +int32_t UsbServerStub::DoRequestQueue(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0, ifId = 0, endpoint; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, ifId, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, endpoint, E_WRITE_PARCEL_ERROR); + uint32_t clientLength = 0; + READ_PARCEL_WITH_RET(data, Uint32, clientLength, E_WRITE_PARCEL_ERROR); + const uint8_t *clientObj = data.ReadBuffer(clientLength); + uint32_t length; + READ_PARCEL_WITH_RET(data, Uint32, length, E_WRITE_PARCEL_ERROR); + const uint8_t *buffer = data.ReadBuffer(length); + //PrintBuffer("UsbServerStub::DoRequestQueue buffer", buffer, length); + return RequestQueue(busNum, devAddr, ifId, endpoint, (void *)clientObj, clientLength, (uint8_t*)buffer, length); +} + +int32_t UsbServerStub::DoRequestWait(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + uint8_t *clientObj = nullptr, *buffer = nullptr; + uint32_t clientLength, length = 0; + int32_t timeout; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Int32, timeout, E_WRITE_PARCEL_ERROR); + clientLength = MAX_REQUEST_BUFFER_SIZE; + clientObj = new uint8_t[clientLength]; + length = MAX_REQUEST_BUFFER_SIZE; + buffer = new uint8_t[length]; + int32_t ret = RequestWait(busNum, devAddr, timeout, (void *)clientObj, clientLength, buffer, length); + if(ret != ERR_OK) { + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s:%{public}d failed ret:%{public}d", __func__, __LINE__, ret); + delete [] clientObj; + delete [] buffer; + return ret; + } + + //PrintBuffer("UsbServerStub::DoRequestWait buffer", buffer, length); + SetBufferMessage(reply, clientObj, clientLength); + SetBufferMessage(reply, buffer, length); + delete [] clientObj; + delete [] buffer; + return ERR_OK; +} + +int32_t UsbServerStub::DoRequestCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + uint8_t interfaceId, endpointId; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, interfaceId, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, endpointId, E_WRITE_PARCEL_ERROR); + int32_t ret = RequestCancel(busNum, devAddr, interfaceId, endpointId); + return ret; +} + +int32_t UsbServerStub::DoClose(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + int32_t ret = Close(busNum, devAddr); + return ret; +} + +int32_t UsbServerStub::DoGetDevices(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + int32_t ret = -1; + std::vector deviceList; + ret = GetDevices(deviceList); + if(ret != ERR_OK) { + USB_HILOGE(MODULE_SERVICE, "%{public}s: failed ret = %{public}d", __func__, ret); + return ret; + } + USB_HILOGE(MODULE_SERVICE, "%{public}s: list size = %{public}d", __func__, deviceList.size()); + //ret = SetDevicesMapToMessageParcel(devices, data); + ret = SetDeviceListMessageParcel(deviceList, reply); + return ret; +} + +int32_t UsbServerStub::DoGetDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + uint8_t busNum = 0, devAddr = 0; + int32_t ret = -1; + READ_PARCEL_WITH_RET(data, Uint8, busNum, E_WRITE_PARCEL_ERROR); + READ_PARCEL_WITH_RET(data, Uint8, devAddr, E_WRITE_PARCEL_ERROR); + + UsbDevice devInfo; + GetDevice(busNum, devAddr, devInfo); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s UsbServerProxy devInfo=%{public}s", __func__, devInfo.ToString().c_str()); + ret = SetDeviceMessageParcel(devInfo, reply); + return ret; +} + +int32_t UsbServerStub::SetDeviceListMessageParcel(std::vector &deviceList, MessageParcel &data) +{ + int32_t deviceCount = (int32_t)deviceList.size(); + data.WriteInt32(deviceCount); + for(int32_t i = 0; i < deviceCount; ++i) { + UsbDevice &devInfo = deviceList[i]; + SetDeviceMessageParcel(devInfo, data); + } + return ERR_OK; +} + + +int32_t UsbServerStub::SetDeviceMessageParcel(UsbDevice &devInfo, MessageParcel &data) +{ + data.WriteInt32(devInfo.GetBusNum()); + data.WriteInt32(devInfo.GetDevAddr()); + + data.WriteInt32(devInfo.GetVendorId()); + data.WriteInt32(devInfo.GetProductId()); + data.WriteInt32(devInfo.GetClass()); + data.WriteInt32(devInfo.GetSubclass()); + data.WriteInt32(devInfo.GetProtocol()); + data.WriteUint8(devInfo.GetiManufacturer()); + data.WriteUint8(devInfo.GetiProduct()); + data.WriteUint8(devInfo.GetiSerialNumber()); + data.WriteUint8(devInfo.GetbMaxPacketSize0()); + data.WriteUint16(devInfo.GetbcdUSB()); + data.WriteUint16(devInfo.GetbcdDevice()); + data.WriteString(devInfo.GetName()); + data.WriteString(devInfo.GetManufacturerName()); + data.WriteString(devInfo.GetProductName()); + data.WriteString(devInfo.GetVersion()); + data.WriteString(devInfo.GetmSerial()); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:%{public}d devName:%{public}s Bus:%{public}d dev:%{public}d ", + __func__, __LINE__, devInfo.GetName().c_str(), devInfo.GetBusNum(), devInfo.GetDevAddr()); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:%{public}d devInfo:%{public}s", + __func__, __LINE__, devInfo.ToString().c_str()); + data.WriteInt32(devInfo.GetConfigCount()); + SetDeviceConfigsMessageParcel(devInfo.GetConfigs(), data); + return ERR_OK; +} + +int32_t UsbServerStub::SetDeviceConfigsMessageParcel(std::vector &configs, MessageParcel &data) +{ + for(auto it = configs.begin(); it != configs.end(); ++it) { + USBConfig config = *it; + data.WriteInt32(config.GetId()); + data.WriteInt32(config.GetAttributes()); + data.WriteInt32(config.GetMaxPower()); + + data.WriteUint8(config.GetiConfiguration()); + data.WriteString(config.GetName()); + + data.WriteInt32(config.GetInterfaceCount()); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s devInfo=%{public}s", __func__, config.ToString().c_str()); + SetDeviceInterfacesMessageParcel(config.GetInterfaces(), data); + } + + return ERR_OK; +} + +int32_t UsbServerStub::SetDeviceInterfacesMessageParcel(std::vector &interfaces, MessageParcel &data) +{ + for(auto it = interfaces.begin(); it != interfaces.end(); ++it) { + UsbInterface interface = *it; + data.WriteInt32(interface.GetId()); + data.WriteInt32(interface.GetClass()); + data.WriteInt32(interface.GetSubClass()); + data.WriteInt32(interface.GetAlternateSetting()); + data.WriteInt32(interface.GetProtocol()); + + data.WriteUint8(interface.GetiInterface()); + data.WriteString(interface.GetName()); + + data.WriteInt32(interface.GetEndpointCount()); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s devInfo=%{public}s", __func__, interface.ToString().c_str()); + SetDeviceEndpointsMessageParcel(interface.GetEndpoints(), data); + } + return ERR_OK; +} + +int32_t UsbServerStub::SetDeviceEndpointsMessageParcel(std::vector &eps, MessageParcel &data) +{ + for(auto it = eps.begin(); it != eps.end(); ++it) { + USBEndpoint ep = *it; + data.WriteInt32(ep.GetAddress()); + data.WriteInt32(ep.GetAttributes()); + data.WriteInt32(ep.GetInterval()); + data.WriteInt32(ep.GetMaxPacketSize()); + USB_HILOGI(MODULE_USB_SERVICE, "%{public}s devInfo=%{public}s", __func__, ep.ToString().c_str()); + } + return ERR_OK; +} + +} // namespace USB +} // namespace OHOS diff --git a/test/native/BUILD.gn b/test/native/BUILD.gn new file mode 100644 index 00000000..7284925b --- /dev/null +++ b/test/native/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") +import("//build/test.gni") + +group("usbmgr_native_test") { + testonly = true + deps = [ "unittest:unittest" ] +} diff --git a/test/native/unittest/BUILD.gn b/test/native/unittest/BUILD.gn new file mode 100644 index 00000000..fe13f76b --- /dev/null +++ b/test/native/unittest/BUILD.gn @@ -0,0 +1,72 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") +import("//build/test.gni") + +module_output_path = "${usb_manager_native_part_name}/usb_manager_native" + +############################################################################### +config("module_private_config") { + visibility = [ ":*" ] + + include_dirs = [ + "include", + "//utils/system/safwk/native/include", + "${usb_manager_path}/hdi/api/include", + "${usb_manager_path}/hdi/client/include", + ] +} + +deps_ex = [ + "aafwk_standard:base", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:libeventhandler", + "ipc:ipc_core", + "hiviewdfx_hilog_native:libhilog", + "safwk:system_ability_fwk", + "samgr_L2:samgr_proxy", +] + + +##############################unittest########################################## +ohos_unittest("test_usb_mgr_service") { + module_out_path = module_output_path + + sources = [ "src/usb_service_test.cpp" ] + + configs = [ + "${utils_path}:utils_config", + ":module_private_config", + ] + + deps = [ + "//base/usb/usb_manager/interfaces/innerkits:usbsrv_client", + "//base/usb/usb_manager/services:usbservice", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = deps_ex +} + +group("unittest") { + testonly = true + deps = [ + ":test_usb_mgr_service", + ] +} + + + diff --git a/test/native/unittest/include/usb_service_test.h b/test/native/unittest/include/usb_service_test.h new file mode 100644 index 00000000..9055be77 --- /dev/null +++ b/test/native/unittest/include/usb_service_test.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_SERVICE_TEST_H +#define USB_SERVICE_TEST_H + +#include +#include + +class UsbServiceTest : public testing::Test { +public: + void HasRight(std::string deviceName) {} +}; +#endif // USB_SERVICE_TEST_H diff --git a/test/native/unittest/src/usb_service_test.cpp b/test/native/unittest/src/usb_service_test.cpp new file mode 100644 index 00000000..619d27bc --- /dev/null +++ b/test/native/unittest/src/usb_service_test.cpp @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2021 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. + */ + +#include "usb_service_test.h" + +#include +#include + +#include "usb_service.h" + +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::USB; +using namespace std; + + +/** + * @tc.name: DisplayMgrService01 + * @tc.desc: Test DisplayMgrService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService01, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService01 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; +} + +/** + * @tc.name: UsbService02 + * @tc.desc: Test UsbService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService02, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService02 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; + + sptr proxy_ = iface_cast(remoteObject_); + std::map devs = proxy_->GetDevices(); + EXPECT_TRUE(devs.size() == 0) << "UsbService02 Devices count is 0"; + + cout << "Please insert a USB device\nPress any key to continue..." << endl; + //system("pause"); + int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs1 = proxy_->GetDevices(); + EXPECT_TRUE(devs1.size() != 0) << "UsbService02 Devices count is not 0"; + + cout << "Please unplug the USB device\nPress any key to continue..." << endl; + //system("pause"); + //int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs2 = proxy_->GetDevices(); + EXPECT_TRUE(devs2.size() == 0) << "UsbService02 Devices count is 0"; +} + +/** + * @tc.name: UsbService03 + * @tc.desc: Test UsbService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService03, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService03 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; + + sptr proxy_ = iface_cast(remoteObject_); + cout << "Please insert a normal USB device\nPress any key to continue..." << endl; + //system("pause"); + int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs = proxy_->GetDevices(); + EXPECT_TRUE(devs.size() != 0) << "UsbService03 Devices count is not 0"; + std::map::iterator iter = devs.begin(); + EXPECT_TRUE(iter->second->GetHasMidi() == false) << "HasMidi is false"; + EXPECT_TRUE(iter->second->GetHasAudioCapture() == false) << "HasAudioCapture is false"; + EXPECT_TRUE(iter->second->GetHasAudioPlayback() == false) << "HasAudioPlayback is false"; + EXPECT_TRUE(iter->second->GetHasVideoCapture() == false) << "HasVideoCapture is false"; + EXPECT_TRUE(iter->second->GetHasVideoPlayback() == false) << "HasVideoPlayBack is false"; +} + +/** + * @tc.name: UsbService04 + * @tc.desc: Test UsbService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService04, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService04 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; + + sptr proxy_ = iface_cast(remoteObject_); + cout << "Please insert a Midi USB device\nPress any key to continue..." << endl; + //system("pause"); + int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs = proxy_->GetDevices(); + EXPECT_TRUE(devs.size() != 0) << "UsbService04 Devices count is not 0"; + std::map::iterator iter = devs.begin(); + EXPECT_TRUE(iter->second->GetHasMidi() == true) << "HasMidi is true"; +} + +/** + * @tc.name: UsbService05 + * @tc.desc: Test UsbService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService05, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService05 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; + + sptr proxy_ = iface_cast(remoteObject_); + cout << "Please insert a USB device with no AudioPlayback function\nPress any key to continue..." << endl; + //system("pause"); + int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs = proxy_->GetDevices(); + EXPECT_TRUE(devs.size() != 0) << "UsbService05 Devices count is not 0"; + std::map::iterator iter = devs.begin(); + EXPECT_TRUE(iter->second->GetHasAudioCapture() == true) << "HasAudioCapture is true"; + EXPECT_TRUE(iter->second->GetHasAudioPlayback() == false) << "HasAudioPlayback is false"; +} + +/** + * @tc.name: UsbService06 + * @tc.desc: Test UsbService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService06, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService06 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; + + sptr proxy_ = iface_cast(remoteObject_); + cout << "Please insert a USB device with AudioPlayback function\nPress any key to continue..." << endl; + //system("pause"); + int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs = proxy_->GetDevices(); + EXPECT_TRUE(devs.size() != 0) << "UsbService06 Devices count is not 0"; + std::map::iterator iter = devs.begin(); + EXPECT_TRUE(iter->second->GetHasAudioCapture() == true) << "HasAudioCapture is true"; + EXPECT_TRUE(iter->second->GetHasAudioPlayback() == true) << "HasAudioPlayback is true"; +} + + +/** + * @tc.name: UsbService07 + * @tc.desc: Test UsbService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService07, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService07 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; + + sptr proxy_ = iface_cast(remoteObject_); + cout << "Please insert a USB device with no VideoPlayback function\nPress any key to continue..." << endl; + //system("pause"); + int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs = proxy_->GetDevices(); + EXPECT_TRUE(devs.size() != 0) << "UsbService07 Devices count is not 0"; + std::map::iterator iter = devs.begin(); + EXPECT_TRUE(iter->second->GetHasVideoCapture() == true) << "HasVideoCapture is true"; + EXPECT_TRUE(iter->second->GetHasVideoPlayback() == false) << "HasVideoPlayBack is false"; +} + +/** + * @tc.name: UsbService08 + * @tc.desc: Test UsbService service ready. + * @tc.type: FUNC + */ +HWTEST_F(UsbServiceTest, UsbService08, TestSize.Level1) +{ + sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sam != nullptr) << "UsbService08 fail to get GetSystemAbilityManager"; + sptr remoteObject_ = sam->CheckSystemAbility(USB_MANAGER_USB_SERVICE_ID); + EXPECT_TRUE(remoteObject_ != nullptr) << "GetSystemAbility failed."; + + sptr proxy_ = iface_cast(remoteObject_); + cout << "Please insert a USB device with VideoPlayback function\nPress any key to continue..." << endl; + //system("pause"); + int c; + while((c = getchar()) != '\n' && c != EOF); + std::map devs = proxy_->GetDevices(); + EXPECT_TRUE(devs.size() != 0) << "UsbService08 Devices count is not 0"; + std::map::iterator iter = devs.begin(); + EXPECT_TRUE(iter->second->GetHasVideoCapture() == true) << "HasVideoCapture is true"; + EXPECT_TRUE(iter->second->GetHasVideoPlayback() == true) << "HasVideoPlayBack is true"; +} + + + + + + + + diff --git a/usbmgr.gni b/usbmgr.gni new file mode 100644 index 00000000..b00cad92 --- /dev/null +++ b/usbmgr.gni @@ -0,0 +1,18 @@ +# Copyright (c) 2021 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. + +import("//build/ohos.gni") + +usb_manager_path = "//base/usb/usb_manager" +usb_manager_native_part_name = "usb_manager_native" +utils_path = "${usb_manager_path}/utils" diff --git a/utils/BUILD.gn b/utils/BUILD.gn new file mode 100644 index 00000000..2d3f04e6 --- /dev/null +++ b/utils/BUILD.gn @@ -0,0 +1,21 @@ +# Copyright (c) 2021 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. + +import("//base/usb/usb_manager/usbmgr.gni") + +config("utils_config") { + include_dirs = [ + "native/include", + "//utils/native/base/include", + ] +} diff --git a/utils/native/include/hilog_wrapper.h b/utils/native/include/hilog_wrapper.h new file mode 100644 index 00000000..d0324d8a --- /dev/null +++ b/utils/native/include/hilog_wrapper.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef HILOG_WRAPPER_H +#define HILOG_WRAPPER_H + +#define CONFIG_HILOG +#ifdef CONFIG_HILOG +#include "hilog/log.h" +namespace OHOS { +namespace USB { +#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) +#define __FORMATED(fmt, ...) "[%{public}s] %{public}s# " fmt, __FILENAME__, __FUNCTION__, ##__VA_ARGS__ + +#ifdef USB_HILOGF +#undef USB_HILOGF +#endif + +#ifdef USB_HILOGE +#undef USB_HILOGE +#endif + +#ifdef USB_HILOGW +#undef USB_HILOGW +#endif + +#ifdef USB_HILOGI +#undef USB_HILOGI +#endif + +#ifdef USB_HILOGD +#undef USB_HILOGD +#endif + +// param of log interface, such as USB_HILOGF. +enum UsbMgrSubModule { + MODULE_INNERKIT = 0, + MODULE_SERVICE, + MODULE_JAVAKIT, // java kit used, define to avoid repeat used domain + MODULE_JNI, + MODULE_USB_INNERKIT, // below used by usb service + MODULE_USB_SERVICE, + MODULE_USBD, + MODULE_COMMON, // + MODULE_JS_NAPI, + USBMGR_MODULE_BUTT, +}; + +// 0xD002900: subsystem:Usb module:Usb, reserved 8 bit. +static constexpr unsigned int BASE_USB_DOMAIN_ID = 0xD002900; + +enum UsbMgrDomainId { + USBMGR_INNERKIT_DOMAIN = BASE_USB_DOMAIN_ID + MODULE_INNERKIT, + USBMGR_SERVICE_DOMAIN, + USBMGR_JAVAKIT_DOMAIN, // 0xD002902 + USB_INNERKIT_DOMAIN, + USB_SERVICE_DOMAIN, + USBD_DOMAIN, + COMMON_DOMAIN, + USB_JS_NAPI, + USB_BUTT, +}; + +static constexpr OHOS::HiviewDFX::HiLogLabel USB_MGR_LABEL[USBMGR_MODULE_BUTT] = { + {LOG_CORE, USBMGR_INNERKIT_DOMAIN, "UsbMgrClient"}, + {LOG_CORE, USBMGR_SERVICE_DOMAIN, "UsbMgrService"}, + {LOG_CORE, USBMGR_JAVAKIT_DOMAIN, "UsbMgrJavaService"}, + {LOG_CORE, USBMGR_INNERKIT_DOMAIN, "UsbMgrJni"}, + {LOG_CORE, USB_INNERKIT_DOMAIN, "UsbSrvClient"}, + {LOG_CORE, USB_SERVICE_DOMAIN, "UsbService"}, + {LOG_CORE, USBD_DOMAIN, "Usbd"}, + {LOG_CORE, COMMON_DOMAIN, "UsbMgrCommon"}, + {LOG_CORE, USB_JS_NAPI, "UsbMgrJSNAPI"}, +}; + +// In order to improve performance, do not check the module range, module should less than USBMGR_MODULE_BUTT. +#define USB_HILOGF(module, ...) (void)OHOS::HiviewDFX::HiLog::Fatal(USB_MGR_LABEL[module], __FORMATED(__VA_ARGS__)) +#define USB_HILOGE(module, ...) (void)OHOS::HiviewDFX::HiLog::Error(USB_MGR_LABEL[module], __FORMATED(__VA_ARGS__)) +#define USB_HILOGW(module, ...) (void)OHOS::HiviewDFX::HiLog::Warn(USB_MGR_LABEL[module], __FORMATED(__VA_ARGS__)) +#define USB_HILOGI(module, ...) (void)OHOS::HiviewDFX::HiLog::Info(USB_MGR_LABEL[module], __FORMATED(__VA_ARGS__)) +#define USB_HILOGD(module, ...) (void)OHOS::HiviewDFX::HiLog::Debug(USB_MGR_LABEL[module], __FORMATED(__VA_ARGS__)) + +} // namespace Usb +} // namespace OHOS + +#else + +#define USB_HILOGF(...) +#define USB_HILOGE(...) +#define USB_HILOGW(...) +#define USB_HILOGI(...) +#define USB_HILOGD(...) + +#endif // CONFIG_HILOG + +#endif // HILOG_WRAPPER_H diff --git a/utils/native/include/sp_singleton.h b/utils/native/include/sp_singleton.h new file mode 100644 index 00000000..3cdc2298 --- /dev/null +++ b/utils/native/include/sp_singleton.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef SP_SINGLETON_H +#define SP_SINGLETON_H + +#include "nocopyable.h" +#include +#include +#include + +namespace OHOS { +namespace USB { + +#define DECLARE_DELAYED_SP_SINGLETON(MyClass) \ +public: \ + ~MyClass(); \ +private: \ + friend DelayedSpSingleton; \ + MyClass(); + +template +class DelayedSpSingleton : public NoCopyable { +public: + static sptr GetInstance(); + static void DestroyInstance(); + +private: + static sptr instance_; + static std::mutex mutex_; +}; + +template +sptr DelayedSpSingleton::instance_ = nullptr; + +template +std::mutex DelayedSpSingleton::mutex_; + +template +sptr DelayedSpSingleton::GetInstance() +{ + if (!instance_) { + std::lock_guard lock(mutex_); + if (instance_ == nullptr) { + instance_ = new T(); + } + } + + return instance_; +} + +template +void DelayedSpSingleton::DestroyInstance() +{ + std::lock_guard lock(mutex_); + if (instance_) { + instance_.clear(); + instance_ = nullptr; + } +} + +} // namespace Usb +} // namespace OHOS + +#endif // SP_SINGLETON_H diff --git a/utils/native/include/usb_common.h b/utils/native/include/usb_common.h new file mode 100644 index 00000000..9cf31b68 --- /dev/null +++ b/utils/native/include/usb_common.h @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_COMMON_H +#define USB_COMMON_H + +#include +#include + +#include "hilog_wrapper.h" +#include "usb_errors.h" + +namespace OHOS { +namespace USB { + /** + * Invalid usb int value + */ + constexpr int32_t INVALID_USB_INT_VALUE = -1; + + /** + * Bitmask used for extracting the USBEndpoint type from it's address + */ + constexpr int32_t USB_ENDPOINT_XFERTYPE_MASK = 0x03; + + /** + * Control USBEndpoint type + */ + constexpr int32_t USB_ENDPOINT_XFER_CONTROL = 0; + + /** + * Isochronous USBEndpoint type + */ + constexpr int32_t USB_ENDPOINT_XFER_ISOC = 1; + + /** + * Bulk USBEndpoint type + */ + constexpr int32_t USB_ENDPOINT_XFER_BULK = 2; + + /** + * Interrupt USBEndpoint type + */ + constexpr int32_t USB_ENDPOINT_XFER_INT = 3; + + /** + * Bitmask used for extracting the USBEndpoint number from it's address + */ + constexpr int32_t USB_ENDPOINT_NUMBER_MASK = 0x0f; + + /** + * Bitmask used for extracting the USBEndpoint direction from it's address + */ + constexpr int32_t USB_ENDPOINT_DIR_MASK = 0x80; + + /** + * Used to signify direction of data for USBEndpoint is OUT, host to device + */ + constexpr int32_t USB_ENDPOINT_DIR_OUT = 0; + + /** + * Used to signify direction of data for USBEndpoint is IN, device to host + */ + constexpr int32_t USB_ENDPOINT_DIR_IN = 0x80; + + /** + * Bitmask for self power in the USBConfig + */ + constexpr int32_t USB_CFG_SELF_POWERED = 0x80; + + /** + * Bitmask for remote wakeup in the USBConfig + */ + constexpr int32_t USB_CFG_REMOTE_WAKEUP = 0x20; + +#define INVALID_STRING_VALUE ("") +#define RETURN_IF_WITH_RET(cond, retval) if (cond) {return (retval);} +#define RETURN_IF(cond) if (cond) {return;} +#define RETURN_IF_WITH_LOG(cond, loginfo) \ + do { \ + if (cond) { \ + USB_HILOGE(MODULE_COMMON, "%{public}s "#loginfo" ", __func__); \ + return; \ + } \ + } while (0) \ + +#define READ_PARCEL_NO_RET(parcel, type, out) \ + do { \ + if (!(parcel).Read##type(out)) { \ + USB_HILOGE(MODULE_COMMON, "%{public}s read "#out" failed", __func__); \ + return; \ + } \ + } while (0) \ + +#define WRITE_PARCEL_NO_RET(parcel, type, data) \ + do { \ + if (!(parcel).Write##type(data)) { \ + USB_HILOGE(MODULE_COMMON, "%{public}s write "#data" failed", __func__); \ + return; \ + } \ + } while (0) \ + +#define READ_PARCEL_WITH_RET(parcel, type, out, retval) \ + do { \ + if (!(parcel).Read##type(out)) { \ + USB_HILOGE(MODULE_COMMON, "%{public}s read "#out" failed", __func__); \ + return (retval); \ + } \ + } while (0) \ + +#define WRITE_PARCEL_WITH_RET(parcel, type, data, retval) \ + do { \ + if (!(parcel).Write##type(data)) { \ + USB_HILOGE(MODULE_COMMON, "%{public}s write "#data" failed", __func__); \ + return (retval); \ + } \ + } while (0) + +template +constexpr auto ToUnderlying(E e) noexcept +{ + return static_cast>(e); +} +} // namespace Usb +} // namespace OHOS + +#endif // USB_COMMON_H diff --git a/utils/native/include/usb_errors.h b/utils/native/include/usb_errors.h new file mode 100644 index 00000000..bf938c94 --- /dev/null +++ b/utils/native/include/usb_errors.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2021 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. + */ + +#ifndef USB_ERRORS_H +#define USB_ERRORS_H + +#include + +namespace OHOS { +namespace USB { +enum { + /** + * Module type: Usb Manager Service + */ + USB_MODULE_TYPE_SERVICE = 0, + /** + * Module type: Usb Manager Kit + */ + USB_MODULE_TYPE_KIT = 1 +}; + +// offset of usbmgr error, only be used in this file. +constexpr ErrCode USBFWK_SERVICE_ERR_OFFSET = ErrCodeOffset(SUBSYS_USB, USB_MODULE_TYPE_SERVICE); + +enum { + E_WRITE_PARCEL_ERROR = USBFWK_SERVICE_ERR_OFFSET, + E_READ_PARCEL_ERROR, + E_GET_SYSTEM_ABILITY_MANAGER_FAILED, + E_GET_USB_SERVICE_FAILED, + E_ADD_DEATH_RECIPIENT_FAILED, + E_INNER_ERR +}; +} // namespace Usb +} // namespace OHOS + +#endif // USB_ERRORS_H -- Gitee