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 : public DelayedRefSingleton<UsbSrvClient> { 42 DECLARE_DELAYED_REF_SINGLETON(UsbSrvClient) 43 44 public: 45 DISALLOW_COPY_AND_MOVE(UsbSrvClient); 46 47 int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pipe); 48 bool HasRight(std::string deviceName); 49 int32_t RequestRight(std::string deviceName); 50 int32_t RemoveRight(std::string deviceName); 51 int32_t GetDevices(std::vector<UsbDevice> &deviceList); 52 int32_t GetPorts(std::vector<UsbPort> &usbPorts); 53 int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes); 54 int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole); 55 int32_t GetCurrentFunctions(int32_t &funcs); 56 int32_t SetCurrentFunctions(int32_t funcs); 57 int32_t UsbFunctionsFromString(std::string_view funcs); 58 std::string UsbFunctionsToString(int32_t funcs); 59 int32_t ClaimInterface(USBDevicePipe &pip, const UsbInterface &interface, bool force); 60 int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface); 61 int32_t BulkTransfer(USBDevicePipe &pip, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData, 62 int32_t timeOut); 63 int32_t ControlTransfer(USBDevicePipe &pip, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl, 64 std::vector<uint8_t> &bufferData); 65 int32_t SetConfiguration(USBDevicePipe &pip, const USBConfig &config); 66 int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface); 67 int32_t GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData); 68 int32_t GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd); 69 bool Close(const USBDevicePipe &pip); 70 int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeOut, UsbRequest &req); 71 72 int32_t RequestInitialize(UsbRequest &request); 73 int32_t RequestFree(UsbRequest &request); 74 int32_t RequestAbort(UsbRequest &request); 75 int32_t RequestQueue(UsbRequest &request); 76 GetVersion()77 std::string GetVersion() 78 { 79 return SEVVERSION; 80 } 81 82 int32_t RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb); 83 int32_t UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint); 84 int32_t BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 85 int32_t BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 86 int32_t BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint); 87 int32_t AddRight(const std::string &bundleName, const std::string &deviceName); 88 int32_t ManageGlobalInterface(bool disable); 89 int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable); 90 int32_t ManageInterfaceType(InterfaceType interfaceType, bool disable); 91 private: 92 class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { 93 public: 94 UsbSrvDeathRecipient() = default; 95 ~UsbSrvDeathRecipient() override = default; 96 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 97 98 private: 99 DISALLOW_COPY_AND_MOVE(UsbSrvDeathRecipient); 100 }; 101 102 int32_t Connect(); 103 void ResetProxy(const wptr<IRemoteObject> &remote); 104 sptr<IUsbSrv> proxy_ = nullptr; 105 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 106 std::mutex mutex_; 107 }; 108 } // namespace USB 109 } // namespace OHOS 110 111 #endif // USBMGR_USB_SRV_CLIENT_H 112