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 return HDF_SUCCESS;
48 }
49
WritePortFile(int32_t powerRole,int32_t dataRole,int32_t mode)50 int32_t UsbdPort::WritePortFile(int32_t powerRole, int32_t dataRole, int32_t mode)
51 {
52 std::string modeStr;
53
54 if (mode == PORT_MODE_HOST || mode == PORT_MODE_DEVICE) {
55 switch (mode) {
56 case PORT_MODE_HOST:
57 modeStr = PORT_MODE_HOST_STR;
58 UsbdFunction::UsbdSetFunction(USB_FUNCTION_NONE);
59 break;
60 case PORT_MODE_DEVICE:
61 modeStr = PORT_MODE_DEVICE_STR;
62 break;
63 default:
64 break;
65 }
66 }
67 if (modeStr.empty()) {
68 HDF_LOGE("%{public}s: modeStr error", __func__);
69 return HDF_FAILURE;
70 }
71
72 uint32_t len = modeStr.size();
73 if (path_.empty()) {
74 return HDF_FAILURE;
75 }
76
77 char pathBuf[PATH_MAX] = {'\0'};
78 if (realpath(path_.c_str(), pathBuf) == NULL) {
79 HDF_LOGE("%{public}s: path conversion failed", __func__);
80 return HDF_FAILURE;
81 }
82
83 int32_t fd = open(pathBuf, O_WRONLY | O_TRUNC);
84 if (fd < 0) {
85 HDF_LOGE("%{public}s: file open error fd = %{public}d", __func__, fd);
86 return HDF_FAILURE;
87 }
88
89 int32_t ret = write(fd, modeStr.c_str(), len);
90 close(fd);
91 if (ret < 0) {
92 HDF_LOGE("%{public}s: write error", __func__);
93 return HDF_FAILURE;
94 }
95 return HDF_SUCCESS;
96 }
97
setPortPath(const std::string & path)98 void UsbdPort::setPortPath(const std::string &path)
99 {
100 path_ = path;
101 }
102
SetPortInit(int32_t portId,int32_t powerRole,int32_t dataRole)103 int32_t UsbdPort::SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole)
104 {
105 int32_t mode = PORT_MODE_DEVICE;
106 if (IfCanSwitch(portId, powerRole, dataRole)) {
107 return HDF_FAILURE;
108 }
109
110 if (powerRole == POWER_ROLE_SOURCE && dataRole == DATA_ROLE_HOST) {
111 mode = PORT_MODE_HOST;
112 }
113
114 if (powerRole == POWER_ROLE_SINK && dataRole == DATA_ROLE_DEVICE) {
115 mode = PORT_MODE_DEVICE;
116 }
117
118 if (WritePortFile(powerRole, dataRole, mode)) {
119 return HDF_FAILURE;
120 }
121 currentPortInfo_.portId = portId;
122 currentPortInfo_.powerRole = powerRole;
123 currentPortInfo_.dataRole = dataRole;
124 currentPortInfo_.mode = mode;
125 if (currentPortInfo_.mode == PORT_MODE_DEVICE) {
126 UsbdFunction::UsbdSetFunction(USB_FUNCTION_HDC);
127 }
128 return HDF_SUCCESS;
129 }
130
SetPort(int32_t portId,int32_t powerRole,int32_t dataRole,UsbdSubscriber * usbdSubscribers,uint32_t len)131 int32_t UsbdPort::SetPort(int32_t portId, int32_t powerRole, int32_t dataRole,
132 UsbdSubscriber *usbdSubscribers, uint32_t len)
133 {
134 int32_t ret = SetPortInit(portId, powerRole, dataRole);
135 if (ret != HDF_SUCCESS) {
136 HDF_LOGE("%{public}s: SetPortInit failed! ret:%{public}d", __func__, ret);
137 return ret;
138 }
139 for (uint32_t i = 0; i < len; i++) {
140 if (usbdSubscribers[i].subscriber != nullptr) {
141 usbdSubscribers[i].subscriber->PortChangedEvent(currentPortInfo_);
142 }
143 }
144
145 return HDF_SUCCESS;
146 }
147
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)148 int32_t UsbdPort::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
149 {
150 portId = currentPortInfo_.portId;
151 powerRole = currentPortInfo_.powerRole;
152 dataRole = currentPortInfo_.dataRole;
153 mode = currentPortInfo_.mode;
154 return HDF_SUCCESS;
155 }
156
UpdatePort(int32_t mode,const sptr<IUsbdSubscriber> & subscriber)157 int32_t UsbdPort::UpdatePort(int32_t mode, const sptr<IUsbdSubscriber> &subscriber)
158 {
159 switch (mode) {
160 case PORT_MODE_HOST:
161 currentPortInfo_.powerRole = POWER_ROLE_SOURCE;
162 currentPortInfo_.dataRole = DATA_ROLE_HOST;
163 currentPortInfo_.mode = PORT_MODE_HOST;
164 break;
165 case PORT_MODE_DEVICE:
166 currentPortInfo_.powerRole = POWER_ROLE_SINK;
167 currentPortInfo_.dataRole = DATA_ROLE_DEVICE;
168 currentPortInfo_.mode = PORT_MODE_DEVICE;
169 break;
170 default:
171 HDF_LOGE("%{public}s invalid mode:%{public}d", __func__, mode);
172 return HDF_FAILURE;
173 }
174
175 if (subscriber == nullptr) {
176 HDF_LOGE("%{public}s subscriber is nullptr", __func__);
177 return HDF_FAILURE;
178 }
179 subscriber->PortChangedEvent(currentPortInfo_);
180 return HDF_SUCCESS;
181 }
182 } // namespace V1_0
183 } // namespace Usb
184 } // namespace HDI
185 } // namespace OHOS
186