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_PORT_H 17 #define OHOS_HDI_USB_V1_2_USBD_PORT_H 18 19 #include <fcntl.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22 23 #include "usb_host_data.h" 24 #include "usbd.h" 25 #include "v1_0/iusbd_subscriber.h" 26 #include "v1_2/usb_types.h" 27 #include "v2_0/iusbd_subscriber.h" 28 29 #define DEFAULT_PORT_ID 1 30 31 #define PORT_MODE_HOST_STR "host" 32 #define PORT_MODE_DEVICE_STR "device" 33 34 #define POWER_ROLE_NONE 0 35 #define POWER_ROLE_SOURCE 1 36 #define POWER_ROLE_SINK 2 37 #define POWER_ROLE_MAX 3 38 39 #define DATA_ROLE_NONE 0 40 #define DATA_ROLE_HOST 1 41 #define DATA_ROLE_DEVICE 2 42 #define DATA_ROLE_MAX 3 43 44 #define PORT_MODE_NONE 0 45 #define PORT_MODE_DEVICE 1 46 #define PORT_MODE_HOST 2 47 48 using OHOS::HDI::Usb::V1_2::PortInfo; 49 50 using OHOS::HDI::Usb::V1_2::IUsbdSubscriber; 51 52 namespace OHOS { 53 namespace HDI { 54 namespace Usb { 55 namespace V1_2 { 56 class UsbdPort { 57 public: 58 static UsbdPort &GetInstance(); 59 int32_t SetPort(int32_t portId, int32_t powerRole, int32_t dataRole, 60 UsbdSubscriber *usbdSubscribers, uint32_t len); 61 int32_t SetUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole, 62 HDI::Usb::V2_0::UsbdSubscriber *usbdSubscribers, uint32_t len); 63 int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode); 64 int32_t UpdatePort(int32_t mode, const sptr<HDI::Usb::V1_2::IUsbdSubscriber> &subscriber); 65 int32_t UpdateUsbPort(int32_t mode, const sptr<HDI::Usb::V2_0::IUsbdSubscriber> &subscriber); 66 void setPortPath(const std::string &path); 67 68 private: 69 UsbdPort() = default; 70 ~UsbdPort() = default; 71 UsbdPort(const UsbdPort &) = delete; 72 UsbdPort(UsbdPort &&) = delete; 73 UsbdPort &operator=(const UsbdPort &) = delete; 74 UsbdPort &operator=(UsbdPort &&) = delete; 75 76 int32_t IfCanSwitch(int32_t portId, int32_t powerRole, int32_t dataRole); 77 int32_t OpenPortFile(int32_t flags); 78 int32_t WritePortFile(int32_t powerRole, int32_t dataRole, int32_t mode); 79 int32_t ReadPortFile(int32_t &powerRole, int32_t &dataRole, int32_t &mode); 80 int32_t SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole); 81 HDI::Usb::V1_2::PortInfo currentPortInfo_ = {DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE, PORT_MODE_DEVICE}; 82 HDI::Usb::V2_0::PortInfo currentPortInfos_ = {DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE, PORT_MODE_DEVICE}; 83 std::string path_; 84 }; 85 } // namespace V1_2 86 } // namespace Usb 87 } // namespace HDI 88 } // namespace OHOS 89 #endif // OHOS_HDI_USB_V1_2_USBD_PORT_H 90