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