• 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_event_handler.h"
38 #include "usb_server_stub.h"
39 #include "usb_service_subscriber.h"
40 #include "usbd_type.h"
41 #include "v1_0/iusb_interface.h"
42 #include "v1_0/iusbd_bulk_callback.h"
43 #include "v1_0/iusbd_subscriber.h"
44 #include "v1_0/usb_types.h"
45 
46 namespace OHOS {
47 namespace USB {
48 const std::string USB_HOST = "usb_host";
49 const std::string USB_DEVICE = "usb_device";
50 const std::string USB_PORT = "usb_port";
51 const std::string USB_HELP = "-h";
52 class UsbService : public SystemAbility, public UsbServerStub {
53     DECLARE_SYSTEM_ABILITY(UsbService)
54     DECLARE_DELAYED_SP_SINGLETON(UsbService);
55 
56 public:
57     enum UnLoadSaType { UNLOAD_SA_DELAY, UNLOAD_SA_IMMEDIATELY };
58     void OnStart() override;
59     void OnStop() override;
60     int Dump(int fd, const std::vector<std::u16string> &args) override;
61 
IsServiceReady()62     bool IsServiceReady() const
63     {
64         return ready_;
65     }
66 
GetHandler()67     std::shared_ptr<UsbServerEventHandler> GetHandler() const
68     {
69         return handler_;
70     }
71 
72     int32_t SetUsbd(const sptr<HDI::Usb::V1_0::IUsbInterface> &usbd);
73     int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override;
74     bool HasRight(std::string deviceName) override;
75     int32_t RequestRight(std::string deviceName) override;
76     int32_t RemoveRight(std::string deviceName) override;
77     int32_t GetDevices(std::vector<UsbDevice> &deviceList) override;
78     int32_t GetCurrentFunctions(int32_t &funcs) override;
79     int32_t SetCurrentFunctions(int32_t funcs) override;
80     int32_t UsbFunctionsFromString(std::string_view funcs) override;
81     std::string UsbFunctionsToString(int32_t funcs) override;
82     int32_t GetPorts(std::vector<UsbPort> &ports) override;
83     int32_t GetSupportedModes(int32_t portId, int32_t &result) override;
84     int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
85 
86     int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t force) override;
87     int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override;
88     int32_t BulkTransferRead(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
89         std::vector<uint8_t> &bufferData, int32_t timeOut) override;
90     int32_t BulkTransferWrite(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
91         const std::vector<uint8_t> &bufferData, int32_t timeOut) override;
92     int32_t ControlTransfer(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl,
93         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     void UnLoadSelf(UnLoadSaType type);
124     int32_t ManageGlobalInterface(bool disable) override;
125     int32_t ManageDevice(int32_t vendorId, int32_t productId, bool disable) override;
126     int32_t ManageInterfaceType(InterfaceType interfaceType, bool disable) override;
127 private:
128     class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
129     public:
130         explicit SystemAbilityStatusChangeListener(sptr<UsbServiceSubscriber> usbdSubscriber);
131         ~SystemAbilityStatusChangeListener() = default;
132         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
133         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
134 
135     private:
136         sptr<UsbServiceSubscriber> usbdSubscriber_;
137     };
138 
139     class UsbdDeathRecipient : public IRemoteObject::DeathRecipient {
140     public:
141         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
142     };
143 
144 private:
145     bool Init();
146     bool InitUsbd();
147     bool IsCommonEventServiceAbilityExist();
148     bool GetBundleName(std::string &bundleName);
149     std::string GetDeviceVidPidSerialNumber(std::string deviceName);
150     int32_t GetDeviceVidPidSerialNumber(std::string deviceName, std::string& strDesc);
151     int32_t FillDevStrings(UsbDevice &dev);
152     std::string GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, uint8_t idx);
153     int32_t InitUsbRight();
154     void DumpHelp(int32_t fd);
155     int32_t PreManageInterface();
156     bool IsEdmEnabled();
157     int32_t ExecuteManageDevicePolicy(std::vector<UsbDeviceId> &whiteList);
158     int32_t GetEdmPolicy(bool &IsGlobalDisabled, std::unordered_map<InterfaceType, bool> &typeDisableMap,
159         std::vector<UsbDeviceId> &trustUsbDeviceId);
160     int32_t GetUsbPolicy(bool &IsGlobalDisabled, std::unordered_map<InterfaceType, bool> &typeDisableMap,
161         std::vector<UsbDeviceId> &trustUsbDeviceId);
162     void ExecuteStrategy(UsbDevice *devInfo);
163     int32_t GetEdmTypePolicy(sptr<IRemoteObject> remote, std::unordered_map<InterfaceType, bool> &typeDisableMap);
164     int32_t GetEdmGlobalPolicy(sptr<IRemoteObject> remote, bool &IsGlobalDisabled);
165     int32_t GetEdmWhiteListPolicy(sptr<IRemoteObject> remote, std::vector<UsbDeviceId> &trustUsbDeviceId);
166     int32_t ManageInterface(const HDI::Usb::V1_0::UsbDev &dev, uint8_t interfaceId, bool disable);
167     int32_t ManageGlobalInterfaceImpl(bool disable);
168     int32_t ManageDeviceImpl(int32_t vendorId, int32_t productId, bool disable);
169     int32_t ManageInterfaceTypeImpl(InterfaceType interfaceType, bool disable);
170     bool ready_ = false;
171     int32_t commEventRetryTimes_ = 0;
172     std::mutex mutex_;
173     std::shared_ptr<UsbHostManager> usbHostManager_;
174     std::shared_ptr<UsbRightManager> usbRightManager_;
175     std::shared_ptr<UsbPortManager> usbPortManager_;
176     std::shared_ptr<UsbDeviceManager> usbDeviceManager_;
177     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
178     std::shared_ptr<UsbServerEventHandler> handler_;
179     sptr<UsbServiceSubscriber> usbdSubscriber_;
180     sptr<HDI::Usb::V1_0::IUsbdBulkCallback> hdiCb_ = nullptr;
181     sptr<HDI::Usb::V1_0::IUsbInterface> usbd_ = nullptr;
182     std::map<std::string, std::string> deviceVidPidMap_;
183     Utils::Timer unloadSelfTimer_ {"unLoadTimer"};
184     uint32_t unloadSelfTimerId_ {UINT32_MAX};
185     sptr<IRemoteObject::DeathRecipient> recipient_;
186 };
187 } // namespace USB
188 } // namespace OHOS
189 #endif // USBMGR_USB_SERVICE_H
190