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 USB_PORT_MANAGER_H 17 #define USB_PORT_MANAGER_H 18 19 #include <algorithm> 20 #include <map> 21 #include <mutex> 22 #include <string> 23 #include <vector> 24 #include "usb_common.h" 25 #include "usb_port.h" 26 #include "v1_0/iusb_interface.h" 27 #ifdef USB_MANAGER_V2_0 28 #include "v2_0/iusb_port_interface.h" 29 #include "usb_manager_subscriber.h" 30 #include "mem_mgr_proxy.h" 31 #include "mem_mgr_client.h" 32 #include "system_ability_definition.h" 33 #endif // USB_MANAGER_V2_0 34 35 namespace OHOS { 36 namespace USB { 37 class UsbPortManager { 38 public: 39 UsbPortManager(); 40 ~UsbPortManager(); 41 42 void Init(); 43 void Init(int32_t test); 44 #ifdef USB_MANAGER_V2_0 45 bool InitUsbPortInterface(); 46 void Stop(); 47 bool IsReverseCharge(); 48 int32_t BindUsbdSubscriber(const sptr<HDI::Usb::V2_0::IUsbdSubscriber> &subscriber); 49 int32_t UnbindUsbdSubscriber(const sptr<HDI::Usb::V2_0::IUsbdSubscriber> &subscriber); 50 #endif // USB_MANAGER_V2_0 51 int32_t SetUsbd(const sptr<HDI::Usb::V1_0::IUsbInterface> &usbd); 52 int32_t GetPorts(std::vector<UsbPort> &ports); 53 int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes); 54 int32_t QueryPort(); 55 void UpdatePort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode); 56 void UpdatePort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode, int32_t supportedModes); 57 void AddPort(UsbPort &port); 58 void RemovePort(int32_t portId); 59 void GetDumpHelp(int32_t fd); 60 void Dump(int32_t fd, const std::vector<std::string> &args); 61 62 int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole); 63 private: 64 void GetIUsbInterface(); 65 void GetPortsInfo(int32_t fd); 66 void DumpGetSupportPort(int32_t fd); 67 void DumpSetPortRoles(int32_t fd, const std::string &args); 68 void ReportPortRoleChangeSysEvent( 69 int32_t currentPowerRole, int32_t updatePowerRole, int32_t currentDataRole, int32_t updateDataRole); 70 void AddPortInfo(int32_t portId, int32_t supportedModes, 71 int32_t currentMode, int32_t currentDataRole, int32_t currentPowerRole); 72 void AddSupportedMode(); 73 std::mutex mutex_; 74 std::map<int32_t, UsbPort> portMap_; 75 std::map<int32_t, int32_t> supportedModeMap_; 76 sptr<HDI::Usb::V1_0::IUsbInterface> usbd_ = nullptr; 77 #ifdef USB_MANAGER_V2_0 78 sptr<HDI::Usb::V2_0::IUsbPortInterface> usbPortInterface_ = nullptr; 79 sptr<UsbManagerSubscriber> usbManagerSubscriber_; 80 sptr<IRemoteObject::DeathRecipient> recipient_ = nullptr; 81 82 class UsbdPortDeathRecipient : public IRemoteObject::DeathRecipient { 83 public: 84 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 85 }; 86 #endif // USB_MANAGER_V2_0 87 }; 88 } // namespace USB 89 } // namespace OHOS 90 91 #endif 92