• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef USBMGR_USB_SERVICE_H
17 #define USBMGR_USB_SERVICE_H
18 
19 #include <map>
20 #include <vector>
21 #include <unordered_map>
22 #include <iostream>
23 #include <chrono>
24 #include <thread>
25 
26 #include "delayed_sp_singleton.h"
27 #include "iremote_object.h"
28 #include "iusb_srv.h"
29 #include "usb_interface_type.h"
30 #include "system_ability.h"
31 #include "system_ability_status_change_stub.h"
32 #include "timer.h"
33 #include "usb_device_manager.h"
34 #include "usb_host_manager.h"
35 #include "usb_port_manager.h"
36 #include "usb_right_manager.h"
37 #include "usb_server_stub.h"
38 #include "usb_service_subscriber.h"
39 #include "usbd_type.h"
40 #include "v1_1/iusb_interface.h"
41 #include "v1_0/iusbd_bulk_callback.h"
42 #include "v1_0/iusbd_subscriber.h"
43 #include "v1_1/usb_types.h"
44 
45 namespace OHOS {
46 namespace USB {
47 const std::string USB_HOST = "usb_host";
48 const std::string USB_DEVICE = "usb_device";
49 const std::string USB_PORT = "usb_port";
50 const std::string USB_HELP = "-h";
51 class UsbService : public SystemAbility, public UsbServerStub {
52     DECLARE_SYSTEM_ABILITY(UsbService)
53     DECLARE_DELAYED_SP_SINGLETON(UsbService);
54 
55 public:
56     enum UnLoadSaType { UNLOAD_SA_DELAY, UNLOAD_SA_IMMEDIATELY };
57     void OnStart() override;
58     void OnStop() override;
59     int Dump(int fd, const std::vector<std::u16string> &args) override;
60 
IsServiceReady()61     bool IsServiceReady() const
62     {
63         return ready_;
64     }
65 
66     static sptr<UsbService> GetGlobalInstance();
67     int32_t SetUsbd(const sptr<HDI::Usb::V1_1::IUsbInterface> &usbd);
68     int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override;
69     bool CheckDevicePermission(uint8_t busNum, uint8_t devAddr);
70     bool HasRight(std::string deviceName) override;
71     int32_t RequestRight(std::string deviceName) override;
72     int32_t RemoveRight(std::string deviceName) override;
73     int32_t GetDevices(std::vector<UsbDevice> &deviceList) override;
74     int32_t GetCurrentFunctions(int32_t &funcs) override;
75     int32_t SetCurrentFunctions(int32_t funcs) override;
76     int32_t UsbFunctionsFromString(std::string_view funcs) override;
77     std::string UsbFunctionsToString(int32_t funcs) override;
78     int32_t GetPorts(std::vector<UsbPort> &ports) override;
79     int32_t GetSupportedModes(int32_t portId, int32_t &result) override;
80     int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
81 
82     int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t force) override;
83     int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override;
84     int32_t BulkTransferRead(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
85         std::vector<uint8_t> &bufferData, int32_t timeOut) override;
86     int32_t BulkTransferReadwithLength(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
87         int32_t length, std::vector<uint8_t> &bufferData, int32_t timeOut) override;
88     int32_t BulkTransferWrite(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
89         const std::vector<uint8_t> &bufferData, int32_t timeOut) override;
90     int32_t ControlTransfer(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl,
91         std::vector<uint8_t> &bufferData) override;
92     int32_t UsbControlTransfer(const HDI::Usb::V1_0::UsbDev &dev,
93         const HDI::Usb::V1_1::UsbCtrlTransferParams &ctrlParams, std::vector<uint8_t> &bufferData) override;
94     int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override;
95     int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) override;
96     int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) override;
97     int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData) override;
98     int32_t GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd) override;
99     int32_t RequestQueue(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
100         const std::vector<uint8_t> &clientData, const std::vector<uint8_t> &bufferData) override;
101     int32_t RequestWait(const HDI::Usb::V1_0::UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData,
102         std::vector<uint8_t> &bufferData) override;
103     int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) override;
104     int32_t Close(uint8_t busNum, uint8_t devAddr) override;
105     bool AddDevice(uint8_t busNum, uint8_t devAddr);
106     bool DelDevice(uint8_t busNum, uint8_t devAddr);
107     void UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode);
108     void UpdateDeviceState(int32_t status);
109     int32_t GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &dev);
110     int32_t GetDeviceInfoDescriptor(
111         const HDI::Usb::V1_0::UsbDev &uDev, std::vector<uint8_t> &descriptor, UsbDevice &dev);
112     int32_t GetConfigDescriptor(UsbDevice &dev, std::vector<uint8_t> &descriptor);
113 
114     int32_t RegBulkCallback(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe,
115         const sptr<IRemoteObject> &cb) override;
116     int32_t UnRegBulkCallback(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe) override;
117     int32_t BulkRead(
118         const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
119     int32_t BulkWrite(
120         const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
121     int32_t BulkCancel(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe) override;
122     int32_t AddRight(const std::string &bundleName, const std::string &deviceName) override;
123     int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName) override;
124     void UnLoadSelf(UnLoadSaType type);
125     int32_t ManageGlobalInterface(bool disable) override;
126     int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable) override;
127     int32_t ManageInterfaceStorage(InterfaceType interfaceType, bool disable) override;
128     int32_t ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable) override;
129     int32_t GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, bool &unactivated) override;
130     int32_t GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed) override;
131 
132     bool GetDeviceProductName(const std::string &deviceName, std::string &productName);
133 private:
134     class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
135     public:
136         explicit SystemAbilityStatusChangeListener(sptr<UsbServiceSubscriber> usbdSubscriber);
137         ~SystemAbilityStatusChangeListener() = default;
138         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
139         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
140 
141     private:
142         sptr<UsbServiceSubscriber> usbdSubscriber_;
143     };
144 
145     class UsbdDeathRecipient : public IRemoteObject::DeathRecipient {
146     public:
147         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
148     };
149 
150 private:
151     bool Init();
152     bool InitUsbd();
153     bool IsCommonEventServiceAbilityExist();
154     bool GetBundleName(std::string &bundleName);
155     bool GetCallingInfo(std::string &bundleName, std::string &tokenId, int32_t &userId);
156     bool GetBundleInfo(std::string &tokenId, int32_t &userId);
157     std::string GetDeviceVidPidSerialNumber(std::string deviceName);
158     int32_t GetDeviceVidPidSerialNumber(std::string deviceName, std::string& strDesc);
159     int32_t FillDevStrings(UsbDevice &dev);
160     std::string GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, uint8_t idx);
161     int32_t InitUsbRight();
162     void DumpHelp(int32_t fd);
163     int32_t PreCallFunction();
164     bool IsEdmEnabled();
165     int32_t ExecuteManageDevicePolicy(std::vector<UsbDeviceId> &whiteList);
166     int32_t ExecuteManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable);
167     int32_t GetEdmPolicy(bool &IsGlobalDisabled, std::unordered_map<InterfaceType, bool> &typeDisableMap,
168         std::vector<UsbDeviceId> &trustUsbDeviceId);
169     int32_t GetUsbPolicy(bool &IsGlobalDisabled, std::unordered_map<InterfaceType, bool> &typeDisableMap,
170         std::vector<UsbDeviceId> &trustUsbDeviceId);
171     void ExecuteStrategy(UsbDevice *devInfo);
172     int32_t GetEdmTypePolicy(sptr<IRemoteObject> remote, std::unordered_map<InterfaceType, bool> &typeDisableMap);
173     int32_t GetEdmGlobalPolicy(sptr<IRemoteObject> remote, bool &IsGlobalDisabled);
174     int32_t GetEdmWhiteListPolicy(sptr<IRemoteObject> remote, std::vector<UsbDeviceId> &trustUsbDeviceId);
175     int32_t ManageInterface(const HDI::Usb::V1_0::UsbDev &dev, uint8_t interfaceId, bool disable);
176     int32_t ManageGlobalInterfaceImpl(bool disable);
177     int32_t ManageDeviceImpl(int32_t vendorId, int32_t productId, bool disable);
178     int32_t ManageInterfaceTypeImpl(InterfaceType interfaceType, bool disable);
179     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
180     bool ready_ = false;
181     int32_t commEventRetryTimes_ = 0;
182     std::mutex mutex_;
183     std::mutex hdiCbMutex_;
184     std::shared_ptr<UsbHostManager> usbHostManager_;
185     std::shared_ptr<UsbRightManager> usbRightManager_;
186     std::shared_ptr<UsbPortManager> usbPortManager_;
187     std::shared_ptr<UsbDeviceManager> usbDeviceManager_;
188     sptr<UsbServiceSubscriber> usbdSubscriber_;
189     sptr<HDI::Usb::V1_0::IUsbdBulkCallback> hdiCb_ = nullptr;
190     sptr<HDI::Usb::V1_1::IUsbInterface> usbd_ = nullptr;
191     std::map<std::string, std::string> deviceVidPidMap_;
192     Utils::Timer unloadSelfTimer_ {"unLoadTimer"};
193     uint32_t unloadSelfTimerId_ {UINT32_MAX};
194     sptr<IRemoteObject::DeathRecipient> recipient_;
195 };
196 } // namespace USB
197 } // namespace OHOS
198 #endif // USBMGR_USB_SERVICE_H
199