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_accessory_manager.h" 35 #include "usb_host_manager.h" 36 #include "usb_port_manager.h" 37 #include "usb_right_manager.h" 38 #include "usb_server_stub.h" 39 #include "usb_service_subscriber.h" 40 #include "usbd_type.h" 41 #include "v1_1/iusb_interface.h" 42 #include "v1_0/iusbd_bulk_callback.h" 43 #include "v1_0/iusbd_subscriber.h" 44 #include "v1_1/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 67 static sptr<UsbService> GetGlobalInstance(); 68 int32_t SetUsbd(const sptr<HDI::Usb::V1_1::IUsbInterface> &usbd); 69 int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override; 70 bool CheckDevicePermission(uint8_t busNum, uint8_t devAddr); 71 bool HasRight(std::string deviceName) override; 72 int32_t RequestRight(std::string deviceName) override; 73 int32_t RemoveRight(std::string deviceName) override; 74 int32_t GetDevices(std::vector<UsbDevice> &deviceList) override; 75 int32_t GetCurrentFunctions(int32_t &funcs) override; 76 int32_t SetCurrentFunctions(int32_t funcs) override; 77 int32_t UsbFunctionsFromString(std::string_view funcs) override; 78 std::string UsbFunctionsToString(int32_t funcs) override; 79 int32_t GetPorts(std::vector<UsbPort> &ports) override; 80 int32_t GetSupportedModes(int32_t portId, int32_t &result) override; 81 int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override; 82 83 int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t force) override; 84 int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override; 85 int32_t BulkTransferRead(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe, 86 std::vector<uint8_t> &bufferData, int32_t timeOut) override; 87 int32_t BulkTransferReadwithLength(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe, 88 int32_t length, std::vector<uint8_t> &bufferData, int32_t timeOut) override; 89 int32_t BulkTransferWrite(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe, 90 const std::vector<uint8_t> &bufferData, int32_t timeOut) override; 91 int32_t ControlTransfer(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl, 92 std::vector<uint8_t> &bufferData) override; 93 int32_t UsbControlTransfer(const HDI::Usb::V1_0::UsbDev &dev, 94 const HDI::Usb::V1_1::UsbCtrlTransferParams &ctrlParams, std::vector<uint8_t> &bufferData) override; 95 int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override; 96 int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) override; 97 int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) override; 98 int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData) override; 99 int32_t GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd) override; 100 int32_t RequestQueue(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe, 101 const std::vector<uint8_t> &clientData, const std::vector<uint8_t> &bufferData) override; 102 int32_t RequestWait(const HDI::Usb::V1_0::UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData, 103 std::vector<uint8_t> &bufferData) override; 104 int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) override; 105 int32_t Close(uint8_t busNum, uint8_t devAddr) override; 106 bool AddDevice(uint8_t busNum, uint8_t devAddr); 107 bool DelDevice(uint8_t busNum, uint8_t devAddr); 108 void UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode); 109 void UpdateDeviceState(int32_t status); 110 int32_t GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &dev); 111 int32_t GetDeviceInfoDescriptor( 112 const HDI::Usb::V1_0::UsbDev &uDev, std::vector<uint8_t> &descriptor, UsbDevice &dev); 113 int32_t GetConfigDescriptor(UsbDevice &dev, std::vector<uint8_t> &descriptor); 114 115 int32_t RegBulkCallback(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe, 116 const sptr<IRemoteObject> &cb) override; 117 int32_t UnRegBulkCallback(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe) override; 118 int32_t BulkRead( 119 const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe, sptr<Ashmem> &ashmem) override; 120 int32_t BulkWrite( 121 const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe, sptr<Ashmem> &ashmem) override; 122 int32_t BulkCancel(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe) override; 123 int32_t AddRight(const std::string &bundleName, const std::string &deviceName) override; 124 int32_t AddAccessRight(const std::string &tokenId, const std::string &deviceName) override; 125 void UnLoadSelf(UnLoadSaType type); 126 int32_t ManageGlobalInterface(bool disable) override; 127 int32_t ManageDevice(int32_t vendorId, int32_t productId, 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 int32_t UserChangeProcess(); 134 135 int32_t GetAccessoryList(std::vector<USBAccessory> &accessList) override; 136 int32_t OpenAccessory(const USBAccessory &access, int32_t &fd) override; 137 int32_t CloseAccessory(int32_t fd) override; 138 int32_t AddAccessoryRight(const uint32_t tokenId, const USBAccessory &access) override; 139 int32_t HasAccessoryRight(const USBAccessory &access, bool &result) override; 140 int32_t RequestAccessoryRight(const USBAccessory &access, bool &result) override; 141 int32_t CancelAccessoryRight(const USBAccessory &access) override; 142 private: 143 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 144 public: 145 explicit SystemAbilityStatusChangeListener(sptr<UsbServiceSubscriber> usbdSubscriber); 146 ~SystemAbilityStatusChangeListener() = default; 147 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 148 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 149 150 private: 151 sptr<UsbServiceSubscriber> usbdSubscriber_; 152 }; 153 154 class UsbdDeathRecipient : public IRemoteObject::DeathRecipient { 155 public: 156 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 157 }; 158 159 private: 160 bool Init(); 161 bool InitUsbd(); 162 bool IsCommonEventServiceAbilityExist(); 163 bool GetBundleName(std::string &bundleName); 164 bool GetCallingInfo(std::string &bundleName, std::string &tokenId, int32_t &userId); 165 bool GetBundleInfo(std::string &tokenId, int32_t &userId); 166 std::string GetDeviceVidPidSerialNumber(std::string deviceName); 167 int32_t GetDeviceVidPidSerialNumber(std::string deviceName, std::string& strDesc); 168 int32_t FillDevStrings(UsbDevice &dev); 169 std::string GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, uint8_t idx); 170 int32_t InitUsbRight(); 171 void DumpHelp(int32_t fd); 172 int32_t PreCallFunction(); 173 bool IsEdmEnabled(); 174 int32_t ExecuteManageDevicePolicy(std::vector<UsbDeviceId> &whiteList); 175 int32_t ExecuteManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable); 176 int32_t GetEdmPolicy(bool &IsGlobalDisabled, std::vector<UsbDeviceType> &disableType, 177 std::vector<UsbDeviceId> &trustUsbDeviceId); 178 int32_t GetUsbPolicy(bool &IsGlobalDisabled, std::vector<UsbDeviceType> &disableType, 179 std::vector<UsbDeviceId> &trustUsbDeviceId); 180 void ExecuteStrategy(UsbDevice *devInfo); 181 int32_t GetEdmTypePolicy(sptr<IRemoteObject> remote, std::vector<UsbDeviceType> &disableType); 182 int32_t GetEdmGlobalPolicy(sptr<IRemoteObject> remote, bool &IsGlobalDisabled); 183 int32_t GetEdmStroageTypePolicy(sptr<IRemoteObject> remote, std::vector<UsbDeviceType> &disableType); 184 int32_t GetEdmWhiteListPolicy(sptr<IRemoteObject> remote, std::vector<UsbDeviceId> &trustUsbDeviceId); 185 int32_t ManageInterface(const HDI::Usb::V1_0::UsbDev &dev, uint8_t interfaceId, bool disable); 186 void ExecuteManageDeviceType(const std::vector<UsbDeviceType> &disableType, bool disable, 187 const std::unordered_map<InterfaceType, std::vector<int32_t>> &map, bool isDev); 188 int32_t ManageGlobalInterfaceImpl(bool disable); 189 int32_t ManageDeviceImpl(int32_t vendorId, int32_t productId, bool disable); 190 int32_t ManageInterfaceTypeImpl(InterfaceType interfaceType, bool disable); 191 int32_t ManageDeviceTypeImpl(InterfaceType interfaceType, bool disable); 192 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 193 bool IsCallerValid(); 194 bool ready_ = false; 195 int32_t commEventRetryTimes_ = 0; 196 std::mutex mutex_; 197 std::mutex hdiCbMutex_; 198 std::mutex functionMutex_; 199 std::shared_ptr<UsbHostManager> usbHostManager_; 200 std::shared_ptr<UsbRightManager> usbRightManager_; 201 std::shared_ptr<UsbPortManager> usbPortManager_; 202 std::shared_ptr<UsbDeviceManager> usbDeviceManager_; 203 std::shared_ptr<UsbAccessoryManager> usbAccessoryManager_; 204 sptr<UsbServiceSubscriber> usbdSubscriber_; 205 sptr<HDI::Usb::V1_0::IUsbdBulkCallback> hdiCb_ = nullptr; 206 sptr<HDI::Usb::V1_1::IUsbInterface> usbd_ = nullptr; 207 std::map<std::string, std::string> deviceVidPidMap_; 208 Utils::Timer unloadSelfTimer_ {"unLoadTimer"}; 209 uint32_t unloadSelfTimerId_ {UINT32_MAX}; 210 sptr<IRemoteObject::DeathRecipient> recipient_; 211 }; 212 } // namespace USB 213 } // namespace OHOS 214 #endif // USBMGR_USB_SERVICE_H 215