• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unordered_map>
23 
24 #include "usb_host_data.h"
25 #include "usbd.h"
26 #include "v1_0/iusbd_subscriber.h"
27 #include "v1_2/usb_types.h"
28 #include "v2_0/iusbd_subscriber.h"
29 #include "v2_0/iusb_device_interface.h"
30 #define DEFAULT_PORT_ID 1
31 
32 #define DATA_ROLE_NONE_STR "none"
33 #define DATA_ROLE_UFP_STR   "host"
34 #define DATA_ROLE_DFP_STR "device"
35 
36 #define POWER_ROLE_NONE_STR "none"
37 #define POWER_ROLE_SOURCE_STR "source"
38 #define POWER_ROLE_SINK_STR "sink"
39 
40 #define SUPPORTED_MODES_NONE_STR "none"
41 #define SUPPORTED_MODES_UFP_STR "ufp"
42 #define SUPPORTED_MODES_DFP_STR "dfp"
43 #define SUPPORTED_MODES_UFP_DFP_STR "ufp dfp"
44 
45 #define MODES_NONE_STR "none"
46 #define MODES_UFP_STR "ufp"
47 #define MODES_DFP_STR "dfp"
48 #define MODES_DRP_STR "drp"
49 
50 #define DATA_ROLE_PATH "/data_role"
51 #define POWER_ROLE_PATH "/power_role"
52 #define SUPPORTED_MODES_PATH "/supported_modes"
53 #define MODE_PATH "/mode"
54 
55 enum PortModes {
56     MODES_NONE = 0,
57     MODES_UFP,
58     MODES_DFP,
59     MODES_DRP
60 };
61 enum SupportedMode {
62     SUPPORTED_MODES_NONE = 0,
63     SUPPORTED_MODES_UFP,
64     SUPPORTED_MODES_DFP,
65     SUPPORTED_MODES_UFP_DFP
66 };
67 enum PowerRole {
68     POWER_ROLE_NONE = 0,
69     POWER_ROLE_SOURCE,
70     POWER_ROLE_SINK,
71     POWER_ROLE_MAX
72 };
73 enum DataRole {
74     DATA_ROLE_NONE = 0,
75     DATA_ROLE_HOST,
76     DATA_ROLE_DEVICE,
77     DATA_ROLE_MAX
78 };
79 enum Mode {
80     PORT_MODE_NONE = 0,
81     PORT_MODE_DEVICE,
82     PORT_MODE_HOST
83 };
84 
85 using OHOS::HDI::Usb::V1_2::PortInfo;
86 
87 using OHOS::HDI::Usb::V1_2::IUsbdSubscriber;
88 
89 namespace OHOS {
90 namespace HDI {
91 namespace Usb {
92 namespace V1_2 {
93 class UsbdPort {
94 public:
95     static UsbdPort &GetInstance();
96     int32_t SetPort(int32_t portId, int32_t powerRole, int32_t dataRole,
97         UsbdSubscriber *usbdSubscribers, uint32_t len);
98     int32_t SetUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole,
99         HDI::Usb::V2_0::UsbdSubscriber *usbdSubscribers, uint32_t len);
100     int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
101     int32_t UpdatePort(int32_t mode, const sptr<HDI::Usb::V1_2::IUsbdSubscriber> &subscriber);
102     int32_t UpdateUsbPort(int32_t mode, const sptr<HDI::Usb::V2_0::IUsbdSubscriber> &subscriber);
103     void setPortPath(const std::string &path);
104     int32_t GetSupportedModes(int32_t &supported_modes);
105 
106 private:
107     UsbdPort() = default;
108     ~UsbdPort() = default;
109     UsbdPort(const UsbdPort &) = delete;
110     UsbdPort(UsbdPort &&) = delete;
111     UsbdPort &operator=(const UsbdPort &) = delete;
112     UsbdPort &operator=(UsbdPort &&) = delete;
113 
114     int32_t IfCanSwitch(int32_t portId, int32_t powerRole, int32_t dataRole);
115     int32_t OpenPortFile(int32_t flags, const std::string &subPath);
116     int32_t SwitchFunction(int32_t dataRole);
117     int32_t WritePortFile(int32_t role, const std::string &subPath);
118     int32_t ReadPortFile(int32_t &role, const std::string &subPath);
119     int32_t SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole);
120     int32_t WritePdPortFile(int32_t powerRole, int32_t dataRole);
121     void QueryPdPort(int32_t &powerRole, int32_t &dataRole, int32_t &mode);
122     void UpdatePdPort(int32_t mode, const sptr<IUsbdSubscriber> &subscriber);
123     void UpdatePdPorts(int32_t mode, const sptr<V2_0::IUsbdSubscriber> &subscriber);
124     bool IsHdcOpen();
125     bool IsDevInterfaceConn();
126     int32_t UsbdSetFunction(int32_t func);
127     HDI::Usb::V1_2::PortInfo currentPortInfo_ = {DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE, PORT_MODE_DEVICE};
128     HDI::Usb::V2_0::PortInfo currentPortInfos_ = {DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE, PORT_MODE_DEVICE};
129     std::string path_;
130     std::string PD_V2_0 = "/dev/check_usbmode";
131     std::string DEFAULT_USB_MODE_PATH = "/data/service/el1/public/usb/mode";
132     bool isPdV2_0 = false;
133     sptr<HDI::Usb::V2_0::IUsbDeviceInterface> usbDeviceInterface_ = nullptr;
134 };
135 } // namespace V1_2
136 } // namespace Usb
137 } // namespace HDI
138 } // namespace OHOS
139 #endif // OHOS_HDI_USB_V1_2_USBD_PORT_H
140