1 /* 2 3 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef USBMGR_USB_SRV_CLIENT_H 18 #define USBMGR_USB_SRV_CLIENT_H 19 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <singleton.h> 24 25 #include "iremote_object.h" 26 #include "iusb_server.h" 27 #include "usb_device.h" 28 #include "usb_device_pipe.h" 29 #include "usb_port.h" 30 #include "usb_request.h" 31 #include "usb_interface_type.h" 32 #include "serial_death_monitor.h" 33 #include "usb_server_types.h" 34 namespace OHOS { 35 namespace USB { 36 constexpr uint8_t CLAIM_FORCE_1 = 1; 37 const std::string MAXVERSION = "001"; 38 const std::string SUBVERSION = "001"; 39 const std::string DLPVERSION = "025"; 40 const std::string SEVVERSION = MAXVERSION + "." + SUBVERSION + "." + DLPVERSION; 41 42 43 class UsbSrvClient final { 44 public: 45 DISALLOW_COPY_AND_MOVE(UsbSrvClient); 46 47 static UsbSrvClient& GetInstance(); 48 int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pipe); 49 int32_t ResetDevice(USBDevicePipe &pipe); 50 bool HasRight(std::string deviceName); 51 int32_t RequestRight(std::string deviceName); 52 int32_t RemoveRight(std::string deviceName); 53 int32_t GetDevices(std::vector<UsbDevice> &deviceList); 54 int32_t GetPorts(std::vector<UsbPort> &usbPorts); 55 int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes); 56 int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole); 57 int32_t GetCurrentFunctions(int32_t &funcs); 58 int32_t SetCurrentFunctions(int32_t funcs); 59 int32_t UsbFunctionsFromString(std::string_view funcs); 60 std::string UsbFunctionsToString(int32_t funcs); 61 int32_t ClaimInterface(USBDevicePipe &pipe, const UsbInterface &interface, bool force); 62 int32_t UsbAttachKernelDriver(USBDevicePipe &pipe, const UsbInterface &interface); 63 int32_t UsbDetachKernelDriver(USBDevicePipe &pipe, const UsbInterface &interface); 64 int32_t ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &interface); 65 int32_t BulkTransfer(USBDevicePipe &pipe, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData, 66 int32_t timeOut); 67 int32_t ControlTransfer(USBDevicePipe &pipe, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl, 68 std::vector<uint8_t> &bufferData); 69 int32_t UsbControlTransfer(USBDevicePipe &pipe, const HDI::Usb::V1_2::UsbCtrlTransferParams &ctrlParams, 70 std::vector<uint8_t> &bufferData); 71 int32_t SetConfiguration(USBDevicePipe &pipe, const USBConfig &config); 72 int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface); 73 int32_t GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData); 74 int32_t GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd); 75 bool Close(const USBDevicePipe &pip); 76 int32_t PipeRequestWait(USBDevicePipe &pipe, int64_t timeOut, UsbRequest &req); 77 78 int32_t RequestInitialize(UsbRequest &request); 79 int32_t RequestFree(UsbRequest &request); 80 int32_t RequestAbort(UsbRequest &request); 81 int32_t RequestQueue(UsbRequest &request); 82 int32_t GetDeviceSpeed(USBDevicePipe &pipe, uint8_t &speed); 83 int32_t GetInterfaceActiveStatus(USBDevicePipe &pipe, const UsbInterface &interface, bool &unactivated); 84 GetVersion()85 std::string GetVersion() 86 { 87 return SEVVERSION; 88 } 89 90 int32_t UsbCancelTransfer(USBDevicePipe &pipe, int32_t &endpoint); 91 int32_t UsbSubmitTransfer(USBDevicePipe &pipe, HDI::Usb::V1_2::USBTransferInfo &info, 92 const TransferCallback &cb, sptr<Ashmem> &ashmem); 93 int32_t RegBulkCallback(USBDevicePipe &pipe, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb); 94 int32_t UnRegBulkCallback(USBDevicePipe &pipe, const USBEndpoint &endpoint); 95 int32_t BulkRead(USBDevicePipe &pipe, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 96 int32_t BulkWrite(USBDevicePipe &pipe, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 97 int32_t BulkCancel(USBDevicePipe &pipe, const USBEndpoint &endpoint); 98 int32_t AddRight(const std::string &bundleName, const std::string &deviceName); 99 int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName); 100 int32_t ManageGlobalInterface(bool disable); 101 int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable); 102 int32_t ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable); 103 int32_t ClearHalt(USBDevicePipe &pipe, const USBEndpoint &endpoint); 104 int32_t AddAccessoryRight(const uint32_t tokenId, const USBAccessory &access); 105 int32_t HasAccessoryRight(const USBAccessory &access, bool &result); 106 int32_t RequestAccessoryRight(const USBAccessory &access, bool &result); 107 int32_t CancelAccessoryRight(const USBAccessory &access); 108 int32_t GetAccessoryList(std::vector<USBAccessory> &accessList); 109 int32_t OpenAccessory(const USBAccessory &access, int32_t &fd); 110 int32_t CloseAccessory(const int32_t fd); 111 112 int32_t SerialOpen(int32_t portId); 113 int32_t SerialClose(int32_t portId); 114 int32_t SerialRead(int32_t portId, std::vector<uint8_t> &data, uint32_t bufferSize, 115 uint32_t& actualSize, uint32_t timeout); 116 int32_t SerialWrite(int32_t portId, const std::vector<uint8_t>& data, 117 uint32_t bufferSize, uint32_t& actualSize, uint32_t timeout); 118 int32_t SerialGetAttribute(int32_t portId, UsbSerialAttr& attribute); 119 int32_t SerialSetAttribute(int32_t portId, const UsbSerialAttr& attribute); 120 int32_t SerialGetPortList( 121 std::vector<UsbSerialPort>& serialPortList); 122 int32_t HasSerialRight(int32_t portId, bool &hasRight); 123 int32_t AddSerialRight(uint32_t tokenId, int32_t portId); 124 int32_t CancelSerialRight(int32_t portId); 125 int32_t RequestSerialRight(int32_t portId, bool &hasRight); 126 private: 127 UsbSrvClient(); 128 ~UsbSrvClient(); 129 void UsbDeviceTypeChange(const std::vector<UsbDeviceType> &disableType, 130 std::vector<UsbDeviceTypeInfo> &deviceTypes); 131 void UsbCtrlTransferChange(const HDI::Usb::V1_0::UsbCtrlTransfer ¶m, UsbCtlSetUp &ctlSetup); 132 void UsbCtrlTransferChange(const HDI::Usb::V1_2::UsbCtrlTransferParams ¶m, UsbCtlSetUp &ctlSetup); 133 void UsbTransInfoChange(const HDI::Usb::V1_2::USBTransferInfo ¶m, UsbTransInfo &info); 134 class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { 135 public: 136 UsbSrvDeathRecipient() = default; 137 ~UsbSrvDeathRecipient() override = default; 138 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 139 140 private: 141 DISALLOW_COPY_AND_MOVE(UsbSrvDeathRecipient); 142 }; 143 144 int32_t Connect(); 145 int32_t ConnectUnLocked(); 146 void ResetProxy(const wptr<IRemoteObject> &remote); 147 sptr<IUsbServer> proxy_ = nullptr; 148 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 149 std::mutex mutex_; 150 sptr<SerialDeathMonitor> serialRemote = nullptr; 151 }; 152 } // namespace USB 153 } // namespace OHOS 154 155 #endif // USBMGR_USB_SRV_CLIENT_H 156