1 /* 2 * Copyright (c) 2022-2025 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 OHOS_HDI_USB_V1_2_USBD_PORTS_H 17 #define OHOS_HDI_USB_V1_2_USBD_PORTS_H 18 19 #include <fcntl.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22 #include <map> 23 #include <dirent.h> 24 #include <vector> 25 #include <mutex> 26 27 #include "usb_host_data.h" 28 #include "usbd.h" 29 #include "v1_0/iusbd_subscriber.h" 30 #include "v1_2/usb_types.h" 31 #include "v2_0/iusbd_subscriber.h" 32 #include "usbd_wrapper.h" 33 34 namespace OHOS { 35 namespace HDI { 36 namespace Usb { 37 namespace V1_2 { 38 class UsbdPorts { 39 public: 40 static UsbdPorts &GetInstance(); 41 int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode); 42 int32_t QueryPorts(std::vector<V2_0::UsbPort>& portList); 43 44 int32_t UpdatePort(int32_t mode, const sptr<IUsbdSubscriber>& subscriber); 45 int32_t UpdatePort(int32_t mode, const sptr<OHOS::HDI::Usb::V2_0::IUsbdSubscriber>& subscriber); 46 47 int32_t SetPort(int32_t portId, int32_t powerRole, int32_t dataRole, 48 UsbdSubscriber *usbdSubscribers, uint32_t len); 49 50 int32_t SetPort(int32_t portId, int32_t powerRole, int32_t dataRole, 51 V2_0::UsbdSubscriber *usbdSubscribers, uint32_t len); 52 53 void setPortPath(const std::string &path); 54 55 private: 56 UsbdPorts(); 57 ~UsbdPorts() = default; 58 UsbdPorts(const UsbdPorts &) = delete; 59 UsbdPorts(UsbdPorts &&) = delete; 60 UsbdPorts &operator=(const UsbdPorts &) = delete; 61 UsbdPorts &operator=(UsbdPorts &&) = delete; 62 63 int32_t ParseDirectory(const std::string& path, std::vector<std::string>& portIds, bool flag); 64 int32_t ReadPortInfo(const std::string& portId, V2_0::UsbPort& usbPort); 65 int32_t WritePortInfo(const std::string& portId, const std::string& portAttributeFilePath, std::string& data); 66 int32_t OpenFile(const std::string& path, int32_t flags); 67 int32_t ParsePortAttribute(const std::string& portAttributeFileName, 68 const std::string& buff, V2_0::UsbPort& usbPort); 69 void InitMap(); 70 int32_t ParsePortId(std::string& value); 71 int32_t GetAttributeValue(const std::string& buff, int32_t& outEnumValue); 72 void AddPort(const V2_0::UsbPort &port); 73 bool IsRoleValueLegality(int32_t powerRole, int32_t dataRole); 74 void GetRoleStrValue(int32_t role, std::string& strRole, bool flag); 75 bool IsUpdate(const V2_0::UsbPort& usbPortInfo); 76 void ReportData(const V2_0::UsbPort& usbPort, V2_0::PortInfo& portInfo); 77 void ReportData(const V2_0::UsbPort& usbPort, PortInfo& portInfo); 78 int32_t SetPortInfo(int32_t portId, int32_t powerRole, int32_t dataRole, V2_0::UsbPort& port); 79 bool IsFileFormat(const std::string& dName); 80 bool IsSupportedMode(int32_t portId); 81 82 std::string path_; 83 std::mutex mutex_; 84 std::map<std::string, int32_t> portAttributeMap_; 85 std::map<int32_t, V2_0::UsbPort> portCacheDataMap_; 86 }; 87 } // namespace V1_2 88 } // namespace Usb 89 } // namespace HDI 90 } // namespace OHOS 91 #endif // OHOS_HDI_USB_V1_2_USBD_PORT_H 92