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     int32_t ResetDevice(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 UsbAttachKernelDriver(USBDevicePipe &pip, const UsbInterface &interface);
61     int32_t UsbDetachKernelDriver(USBDevicePipe &pip, const UsbInterface &interface);
62     int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface);
63     int32_t BulkTransfer(USBDevicePipe &pip, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData,
64         int32_t timeOut);
65     int32_t ControlTransfer(USBDevicePipe &pip, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl,
66         std::vector<uint8_t> &bufferData);
67     int32_t UsbControlTransfer(USBDevicePipe &pipe, const HDI::Usb::V1_1::UsbCtrlTransferParams &ctrlParams,
68         std::vector<uint8_t> &bufferData);
69     int32_t SetConfiguration(USBDevicePipe &pip, const USBConfig &config);
70     int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface);
71     int32_t GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData);
72     int32_t GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd);
73     bool Close(const USBDevicePipe &pip);
74     int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeOut, UsbRequest &req);
75 
76     int32_t RequestInitialize(UsbRequest &request);
77     int32_t RequestFree(UsbRequest &request);
78     int32_t RequestAbort(UsbRequest &request);
79     int32_t RequestQueue(UsbRequest &request);
80     int32_t GetDeviceSpeed(USBDevicePipe &pipe, uint8_t &speed);
81     int32_t GetInterfaceActiveStatus(USBDevicePipe &pipe, const UsbInterface &interface, bool &unactivated);
82 
GetVersion()83     std::string GetVersion()
84     {
85         return SEVVERSION;
86     }
87 
88     int32_t RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb);
89     int32_t UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint);
90     int32_t BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem);
91     int32_t BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem);
92     int32_t BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint);
93     int32_t AddRight(const std::string &bundleName, const std::string &deviceName);
94     int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName);
95     int32_t ManageGlobalInterface(bool disable);
96     int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable);
97     int32_t ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable);
98     int32_t ClearHalt(USBDevicePipe &pipe, const USBEndpoint &endpoint);
99 private:
100     UsbSrvClient();
101     ~UsbSrvClient();
102 
103     class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient {
104     public:
105         UsbSrvDeathRecipient() = default;
106         ~UsbSrvDeathRecipient() override = default;
107         void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
108 
109     private:
110         DISALLOW_COPY_AND_MOVE(UsbSrvDeathRecipient);
111     };
112 
113     int32_t Connect();
114     void ResetProxy(const wptr<IRemoteObject> &remote);
115     sptr<IUsbSrv> proxy_ = nullptr;
116     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
117     std::mutex mutex_;
118 };
119 } // namespace USB
120 } // namespace OHOS
121 
122 #endif // USBMGR_USB_SRV_CLIENT_H
123