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 #include "usbd_port.h"
17 #include "hdf_base.h"
18 #include "hdf_log.h"
19 #include "usbd_function.h"
20 #include "usbd_wrapper.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Usb {
25 namespace V1_2 {
GetInstance()26 UsbdPort &UsbdPort::GetInstance()
27 {
28 static UsbdPort instance;
29 return instance;
30 }
31
IfCanSwitch(int32_t portId,int32_t powerRole,int32_t dataRole)32 int32_t UsbdPort::IfCanSwitch(int32_t portId, int32_t powerRole, int32_t dataRole)
33 {
34 if (portId != DEFAULT_PORT_ID) {
35 HDF_LOGE("%{public}s: portId error", __func__);
36 return HDF_FAILURE;
37 }
38
39 if (powerRole <= POWER_ROLE_NONE || powerRole >= POWER_ROLE_MAX) {
40 HDF_LOGE("%{public}s: powerRole error", __func__);
41 return HDF_FAILURE;
42 }
43
44 if (dataRole <= DATA_ROLE_NONE || dataRole >= DATA_ROLE_MAX) {
45 HDF_LOGE("%{public}s: dataRole error", __func__);
46 return HDF_FAILURE;
47 }
48
49 if (path_ == "/data/service/el1/public/usb/mode") {
50 HDF_LOGE("%{public}s: not support", __func__);
51 return HDF_ERR_NOT_SUPPORT;
52 }
53 return HDF_SUCCESS;
54 }
55
OpenPortFile(int32_t flags)56 int32_t UsbdPort::OpenPortFile(int32_t flags)
57 {
58 if (path_.empty()) {
59 return HDF_FAILURE;
60 }
61
62 char pathBuf[PATH_MAX] = {'\0'};
63 if (realpath(path_.c_str(), pathBuf) == NULL) {
64 HDF_LOGE("%{public}s: path conversion failed", __func__);
65 return HDF_FAILURE;
66 }
67
68 return open(pathBuf, flags);
69 }
70
WritePortFile(int32_t powerRole,int32_t dataRole,int32_t mode)71 int32_t UsbdPort::WritePortFile(int32_t powerRole, int32_t dataRole, int32_t mode)
72 {
73 std::string modeStr;
74
75 if (mode == PORT_MODE_HOST || mode == PORT_MODE_DEVICE) {
76 switch (mode) {
77 case PORT_MODE_HOST:
78 modeStr = PORT_MODE_HOST_STR;
79 UsbdFunction::UsbdSetFunction(USB_FUNCTION_NONE);
80 break;
81 case PORT_MODE_DEVICE:
82 modeStr = PORT_MODE_DEVICE_STR;
83 break;
84 default:
85 break;
86 }
87 }
88 if (modeStr.empty()) {
89 HDF_LOGE("%{public}s: modeStr error", __func__);
90 return HDF_FAILURE;
91 }
92
93 uint32_t len = modeStr.size();
94 int32_t fd = OpenPortFile(O_WRONLY | O_TRUNC);
95 if (fd < 0) {
96 HDF_LOGE("%{public}s: file open error fd = %{public}d", __func__, fd);
97 return HDF_FAILURE;
98 }
99
100 int32_t ret = write(fd, modeStr.c_str(), len);
101 close(fd);
102 if (ret < 0) {
103 HDF_LOGE("%{public}s: write error", __func__);
104 return HDF_FAILURE;
105 }
106 return HDF_SUCCESS;
107 }
108
ReadPortFile(int32_t & powerRole,int32_t & dataRole,int32_t & mode)109 int32_t UsbdPort::ReadPortFile(int32_t &powerRole, int32_t &dataRole, int32_t &mode)
110 {
111 int32_t fd = OpenPortFile(O_RDONLY);
112 if (fd < 0) {
113 HDF_LOGE("%{public}s: file open error fd = %{public}d", __func__, fd);
114 return HDF_FAILURE;
115 }
116
117 char modeBuf[PATH_MAX] = {'\0'};
118 int32_t ret = read(fd, modeBuf, PATH_MAX - 1);
119 close(fd);
120
121 if (ret < 0) {
122 HDF_LOGE("%{public}s: read error: %{public}s", __func__, path_.c_str());
123 return HDF_FAILURE;
124 }
125
126 if (strcmp(modeBuf, PORT_MODE_HOST_STR) == 0) {
127 powerRole = POWER_ROLE_SOURCE;
128 dataRole = DATA_ROLE_HOST;
129 mode = PORT_MODE_HOST;
130 return HDF_SUCCESS;
131 }
132
133 if (strcmp(modeBuf, PORT_MODE_DEVICE_STR) == 0) {
134 powerRole = POWER_ROLE_SINK;
135 dataRole = DATA_ROLE_DEVICE;
136 mode = PORT_MODE_DEVICE;
137 return HDF_SUCCESS;
138 }
139
140 HDF_LOGE("%{public}s: read invalid mode: %{public}s", __func__, modeBuf);
141 return HDF_FAILURE;
142 }
143
setPortPath(const std::string & path)144 void UsbdPort::setPortPath(const std::string &path)
145 {
146 path_ = path;
147 HDF_LOGI("%{public}s: port_file_path = %{public}s", __func__, path_.c_str());
148 }
149
SetPortInit(int32_t portId,int32_t powerRole,int32_t dataRole)150 int32_t UsbdPort::SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole)
151 {
152 auto ret = IfCanSwitch(portId, powerRole, dataRole);
153 if (ret != HDF_SUCCESS) {
154 HDF_LOGE("%{public}s: can not switch function", __func__);
155 return ret;
156 }
157
158 int32_t mode = PORT_MODE_DEVICE;
159 if (powerRole == POWER_ROLE_SOURCE && dataRole == DATA_ROLE_HOST) {
160 mode = PORT_MODE_HOST;
161 }
162
163 if (powerRole == POWER_ROLE_SINK && dataRole == DATA_ROLE_DEVICE) {
164 mode = PORT_MODE_DEVICE;
165 }
166
167 if (WritePortFile(powerRole, dataRole, mode)) {
168 return HDF_FAILURE;
169 }
170 currentPortInfo_.portId = portId;
171 currentPortInfo_.powerRole = powerRole;
172 currentPortInfo_.dataRole = dataRole;
173 currentPortInfo_.mode = mode;
174 if (currentPortInfo_.mode == PORT_MODE_DEVICE) {
175 UsbdFunction::UsbdSetFunction(USB_FUNCTION_HDC);
176 }
177 return HDF_SUCCESS;
178 }
179
SetPort(int32_t portId,int32_t powerRole,int32_t dataRole,UsbdSubscriber * usbdSubscribers,uint32_t len)180 int32_t UsbdPort::SetPort(
181 int32_t portId, int32_t powerRole, int32_t dataRole, UsbdSubscriber *usbdSubscribers, uint32_t len)
182 {
183 int32_t ret = SetPortInit(portId, powerRole, dataRole);
184 if (ret != HDF_SUCCESS) {
185 HDF_LOGE("%{public}s: SetPortInit failed! ret:%{public}d", __func__, ret);
186 return ret;
187 }
188 for (uint32_t i = 0; i < len; i++) {
189 if (usbdSubscribers[i].subscriber != nullptr) {
190 usbdSubscribers[i].subscriber->PortChangedEvent(currentPortInfo_);
191 }
192 }
193
194 return HDF_SUCCESS;
195 }
196
SetUsbPort(int32_t portId,int32_t powerRole,int32_t dataRole,HDI::Usb::V2_0::UsbdSubscriber * usbdSubscribers,uint32_t len)197 int32_t UsbdPort::SetUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole,
198 HDI::Usb::V2_0::UsbdSubscriber *usbdSubscribers, uint32_t len)
199 {
200 int32_t ret = SetPortInit(portId, powerRole, dataRole);
201 if (ret != HDF_SUCCESS) {
202 HDF_LOGE("%{public}s: SetPortInit failed! ret:%{public}d", __func__, ret);
203 return ret;
204 }
205 currentPortInfos_ = {currentPortInfo_.portId,
206 currentPortInfo_.powerRole, currentPortInfo_.dataRole, currentPortInfo_.mode};
207 for (uint32_t i = 0; i < len; i++) {
208 if (usbdSubscribers[i].subscriber != nullptr) {
209 usbdSubscribers[i].subscriber->PortChangedEvent(currentPortInfos_);
210 }
211 }
212
213 return HDF_SUCCESS;
214 }
215
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)216 int32_t UsbdPort::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
217 {
218 (void)ReadPortFile(currentPortInfo_.powerRole, currentPortInfo_.dataRole, currentPortInfo_.mode);
219 portId = currentPortInfo_.portId;
220 powerRole = currentPortInfo_.powerRole;
221 dataRole = currentPortInfo_.dataRole;
222 mode = currentPortInfo_.mode;
223 return HDF_SUCCESS;
224 }
225
UpdatePort(int32_t mode,const sptr<IUsbdSubscriber> & subscriber)226 int32_t UsbdPort::UpdatePort(int32_t mode, const sptr<IUsbdSubscriber> &subscriber)
227 {
228 switch (mode) {
229 case PORT_MODE_HOST:
230 currentPortInfo_.powerRole = POWER_ROLE_SOURCE;
231 currentPortInfo_.dataRole = DATA_ROLE_HOST;
232 currentPortInfo_.mode = PORT_MODE_HOST;
233 break;
234 case PORT_MODE_DEVICE:
235 currentPortInfo_.powerRole = POWER_ROLE_SINK;
236 currentPortInfo_.dataRole = DATA_ROLE_DEVICE;
237 currentPortInfo_.mode = PORT_MODE_DEVICE;
238 break;
239 default:
240 HDF_LOGE("%{public}s invalid mode:%{public}d", __func__, mode);
241 return HDF_FAILURE;
242 }
243
244 if (subscriber == nullptr) {
245 HDF_LOGE("%{public}s subscriber is nullptr", __func__);
246 return HDF_FAILURE;
247 }
248 subscriber->PortChangedEvent(currentPortInfo_);
249 return HDF_SUCCESS;
250 }
251
UpdateUsbPort(int32_t mode,const sptr<V2_0::IUsbdSubscriber> & subscriber)252 int32_t UsbdPort::UpdateUsbPort(int32_t mode, const sptr<V2_0::IUsbdSubscriber> &subscriber)
253 {
254 switch (mode) {
255 case PORT_MODE_HOST:
256 currentPortInfo_.powerRole = POWER_ROLE_SOURCE;
257 currentPortInfo_.dataRole = DATA_ROLE_HOST;
258 currentPortInfo_.mode = PORT_MODE_HOST;
259 break;
260 case PORT_MODE_DEVICE:
261 currentPortInfo_.powerRole = POWER_ROLE_SINK;
262 currentPortInfo_.dataRole = DATA_ROLE_DEVICE;
263 currentPortInfo_.mode = PORT_MODE_DEVICE;
264 break;
265 default:
266 HDF_LOGE("%{public}s invalid mode:%{public}d", __func__, mode);
267 return HDF_FAILURE;
268 }
269
270 if (subscriber == nullptr) {
271 HDF_LOGE("%{public}s subscriber is nullptr", __func__);
272 return HDF_FAILURE;
273 }
274 currentPortInfos_ = {currentPortInfo_.portId,
275 currentPortInfo_.powerRole, currentPortInfo_.dataRole, currentPortInfo_.mode};
276 subscriber->PortChangedEvent(currentPortInfos_);
277 return HDF_SUCCESS;
278 }
279 } // namespace V1_2
280 } // namespace Usb
281 } // namespace HDI
282 } // namespace OHOS
283