• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 
3  * Copyright (c) 2021 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 #include "iremote_object.h"
25 #include "iusb_srv.h"
26 #include "usb_device.h"
27 #include "usb_device_pipe.h"
28 #include "usb_port.h"
29 #include "usb_request.h"
30 
31 namespace OHOS {
32 namespace USB {
33 const std::string MAXVERSION = "001";
34 const std::string SUBVERSION = "001";
35 const std::string DLPVERSION = "025";
36 const std::string SEVVERSION = MAXVERSION + "." + SUBVERSION + "." + DLPVERSION;
37 
38 class UsbSrvClient final : public DelayedRefSingleton<UsbSrvClient> {
39     DECLARE_DELAYED_REF_SINGLETON(UsbSrvClient)
40 public:
41     DISALLOW_COPY_AND_MOVE(UsbSrvClient);
42 
43     int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pip);
44     int32_t HasRight(std::string deviceName);
45     int32_t RequestRight(std::string deviceName);
46     int32_t RemoveRight(std::string deviceName);
47     int32_t GetDevices(std::vector<UsbDevice> &deviceList);
48     int32_t GetPorts(std::vector<UsbPort> &usbPorts);
49     int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes);
50     int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole);
51     int32_t GetCurrentFunctions(int32_t &funcs);
52     int32_t SetCurrentFunctions(int32_t funcs);
53     int32_t UsbFunctionsFromString(std::string funcs);
54     std::string UsbFunctionsToString(int32_t funcs);
55     int32_t ClaimInterface(USBDevicePipe &pip, const UsbInterface &interface, bool force);
56     int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface);
57     int32_t BulkTransfer(USBDevicePipe &pip, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData,
58         int32_t timeOut);
59     int32_t ControlTransfer(USBDevicePipe &pip, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData);
60     int32_t SetConfiguration(USBDevicePipe &pip, const USBConfig &config);
61     int32_t SetInterface(USBDevicePipe &pipe, const UsbInterface &interface);
62     int32_t GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData);
63     int32_t GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd);
64     bool Close(const USBDevicePipe &pip);
65     int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeOut, UsbRequest &req);
66 
67     int32_t RequestInitialize(UsbRequest &request);
68     int32_t RequestFree(UsbRequest &request);
69     int32_t RequestAbort(UsbRequest &request);
70     int32_t RequestQueue(UsbRequest &request);
71 
GetVersion()72     std::string GetVersion()
73     {
74         return SEVVERSION;
75     }
76     int32_t RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb);
77     int32_t UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint);
78     int32_t BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem);
79     int32_t BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem);
80     int32_t BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint);
81 
82 private:
83     class UsbSrvDeathRecipient : public IRemoteObject::DeathRecipient {
84     public:
85         UsbSrvDeathRecipient() = default;
86         ~UsbSrvDeathRecipient() = default;
87         void OnRemoteDied(const wptr<IRemoteObject> &remote);
88 
89     private:
90         DISALLOW_COPY_AND_MOVE(UsbSrvDeathRecipient);
91     };
92 
93     int32_t Connect();
94     sptr<IUsbSrv> proxy_ = nullptr;
95     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
96     void ResetProxy(const wptr<IRemoteObject> &remote);
97     std::mutex mutex_;
98 };
99 } // namespace USB
100 } // namespace OHOS
101 
102 #endif // USBMGR_USB_SRV_CLIENT_H
103