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 32 namespace OHOS { 33 namespace USB { 34 constexpr uint8_t CLAIM_FORCE_1 = 1; 35 const std::string MAXVERSION = "001"; 36 const std::string SUBVERSION = "001"; 37 const std::string DLPVERSION = "025"; 38 const std::string SEVVERSION = MAXVERSION + "." + SUBVERSION + "." + DLPVERSION; 39 40 class UsbSrvClient final : public DelayedRefSingleton<UsbSrvClient> { 41 DECLARE_DELAYED_REF_SINGLETON(UsbSrvClient) 42 43 public: 44 DISALLOW_COPY_AND_MOVE(UsbSrvClient); 45 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 SetConfiguration(USBDevicePipe &pip, const USBConfig &config); 65 int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface); 66 int32_t GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData); 67 int32_t GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd); 68 bool Close(const USBDevicePipe &pip); 69 int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeOut, UsbRequest &req); 70 71 int32_t RequestInitialize(UsbRequest &request); 72 int32_t RequestFree(UsbRequest &request); 73 int32_t RequestAbort(UsbRequest &request); 74 int32_t RequestQueue(UsbRequest &request); 75 GetVersion()76 std::string GetVersion() 77 { 78 return SEVVERSION; 79 } 80 81 int32_t RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb); 82 int32_t UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint); 83 int32_t BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 84 int32_t BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem); 85 int32_t BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint); 86 int32_t AddRight(const std::string &bundleName, const std::string &deviceName); 87 88 private: 89 class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient { 90 public: 91 UsbSrvDeathRecipient() = default; 92 ~UsbSrvDeathRecipient() = default; 93 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 94 95 private: 96 DISALLOW_COPY_AND_MOVE(UsbSrvDeathRecipient); 97 }; 98 99 int32_t Connect(); 100 void ResetProxy(const wptr<IRemoteObject> &remote); 101 sptr<IUsbSrv> proxy_ = nullptr; 102 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 103 std::mutex mutex_; 104 }; 105 } // namespace USB 106 } // namespace OHOS 107 108 #endif // USBMGR_USB_SRV_CLIENT_H 109