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_srv.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 33 namespace OHOS { 34 namespace USB { 35 constexpr uint8_t CLAIM_FORCE_1 = 1; 36 const std::string MAXVERSION = "001"; 37 const std::string SUBVERSION = "001"; 38 const std::string DLPVERSION = "025"; 39 const std::string SEVVERSION = MAXVERSION + "." + SUBVERSION + "." + DLPVERSION; 40 41 class UsbSrvClient final { 42 public: 43 DISALLOW_COPY_AND_MOVE(UsbSrvClient); 44 45 static UsbSrvClient& GetInstance(); 46 int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pipe); 47 bool HasRight(std::string deviceName); 48 int32_t RequestRight(std::string deviceName); 49 int32_t RemoveRight(std::string deviceName); 50 int32_t GetDevices(std::vector<UsbDevice> &deviceList); 51 int32_t GetPorts(std::vector<UsbPort> &usbPorts); 52 int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes); 53 int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole); 54 int32_t GetCurrentFunctions(int32_t &funcs); 55 int32_t SetCurrentFunctions(int32_t funcs); 56 int32_t UsbFunctionsFromString(std::string_view funcs); 57 std::string UsbFunctionsToString(int32_t funcs); 58 int32_t ClaimInterface(USBDevicePipe &pip, const UsbInterface &interface, bool force); 59 int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface); 60 int32_t BulkTransfer(USBDevicePipe &pip, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData, 61 int32_t timeOut); 62 int32_t ControlTransfer(USBDevicePipe &pip, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl, 63 std::vector<uint8_t> &bufferData); 64 int32_t UsbControlTransfer(USBDevicePipe &pipe, const HDI::Usb::V1_1::UsbCtrlTransferParams &ctrlParams, 65 std::vector<uint8_t> &bufferData); 66 int32_t SetConfiguration(USBDevicePipe &pip, const USBConfig &config); 67 int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface); 68 int32_t GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData); 69 int32_t GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd); 70 bool Close(const USBDevicePipe &pip); 71 int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeOut, UsbRequest &req); 72 73 int32_t RequestInitialize(UsbRequest &request); 74 int32_t RequestFree(UsbRequest &request); 75 int32_t RequestAbort(UsbRequest &request); 76 int32_t RequestQueue(UsbRequest &request); 77 int32_t GetDeviceSpeed(USBDevicePipe &pipe, uint8_t &speed); 78 int32_t GetInterfaceActiveStatus(USBDevicePipe &pipe, const UsbInterface &interface, bool &unactivated); 79 GetVersion()80 std::string GetVersion() 81 { 82 return SEVVERSION; 83 } 84 85 int32_t RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb); 86 int32_t UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint); 87 int32_t BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 88 int32_t BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 89 int32_t BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint); 90 int32_t AddRight(const std::string &bundleName, const std::string &deviceName); 91 int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName); 92 int32_t ManageGlobalInterface(bool disable); 93 int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable); 94 int32_t ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable); 95 int32_t AddAccessoryRight(const uint32_t tokenId, const USBAccessory &access); 96 int32_t HasAccessoryRight(const USBAccessory &access, bool &result); 97 int32_t RequestAccessoryRight(const USBAccessory &access, bool &result); 98 int32_t CancelAccessoryRight(const USBAccessory &access); 99 int32_t GetAccessoryList(std::vector<USBAccessory> &accessList); 100 int32_t OpenAccessory(const USBAccessory &access, int32_t &fd); 101 int32_t CloseAccessory(const int32_t fd); 102 private: 103 UsbSrvClient(); 104 ~UsbSrvClient(); 105 106 class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { 107 public: 108 UsbSrvDeathRecipient() = default; 109 ~UsbSrvDeathRecipient() override = default; 110 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 111 112 private: 113 DISALLOW_COPY_AND_MOVE(UsbSrvDeathRecipient); 114 }; 115 116 int32_t Connect(); 117 void ResetProxy(const wptr<IRemoteObject> &remote); 118 sptr<IUsbSrv> proxy_ = nullptr; 119 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 120 std::mutex mutex_; 121 }; 122 } // namespace USB 123 } // namespace OHOS 124 125 #endif // USBMGR_USB_SRV_CLIENT_H 126