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 #include "usbd_wrapper.h"
21 
22 namespace OHOS {
23 namespace HDI {
24 namespace Usb {
25 namespace V1_1 {
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 }
148 
SetPortInit(int32_t portId,int32_t powerRole,int32_t dataRole)149 int32_t UsbdPort::SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole)
150 {
151     auto ret = IfCanSwitch(portId, powerRole, dataRole);
152     if (ret != HDF_SUCCESS) {
153         HDF_LOGE("%{public}s: can not switch function", __func__);
154         return ret;
155     }
156 
157     int32_t mode = PORT_MODE_DEVICE;
158     if (powerRole == POWER_ROLE_SOURCE && dataRole == DATA_ROLE_HOST) {
159         mode = PORT_MODE_HOST;
160     }
161 
162     if (powerRole == POWER_ROLE_SINK && dataRole == DATA_ROLE_DEVICE) {
163         mode = PORT_MODE_DEVICE;
164     }
165 
166     if (WritePortFile(powerRole, dataRole, mode)) {
167         return HDF_FAILURE;
168     }
169     currentPortInfo_.portId = portId;
170     currentPortInfo_.powerRole = powerRole;
171     currentPortInfo_.dataRole = dataRole;
172     currentPortInfo_.mode = mode;
173     if (currentPortInfo_.mode == PORT_MODE_DEVICE) {
174         UsbdFunction::UsbdSetFunction(USB_FUNCTION_HDC);
175     }
176     return HDF_SUCCESS;
177 }
178 
SetPort(int32_t portId,int32_t powerRole,int32_t dataRole,UsbdSubscriber * usbdSubscribers,uint32_t len)179 int32_t UsbdPort::SetPort(
180     int32_t portId, int32_t powerRole, int32_t dataRole, UsbdSubscriber *usbdSubscribers, uint32_t len)
181 {
182     int32_t ret = SetPortInit(portId, powerRole, dataRole);
183     if (ret != HDF_SUCCESS) {
184         HDF_LOGE("%{public}s: SetPortInit failed! ret:%{public}d", __func__, ret);
185         return ret;
186     }
187     for (uint32_t i = 0; i < len; i++) {
188         if (usbdSubscribers[i].subscriber != nullptr) {
189             usbdSubscribers[i].subscriber->PortChangedEvent(currentPortInfo_);
190         }
191     }
192 
193     return HDF_SUCCESS;
194 }
195 
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)196 int32_t UsbdPort::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
197 {
198     (void)ReadPortFile(currentPortInfo_.powerRole, currentPortInfo_.dataRole, currentPortInfo_.mode);
199     portId = currentPortInfo_.portId;
200     powerRole = currentPortInfo_.powerRole;
201     dataRole = currentPortInfo_.dataRole;
202     mode = currentPortInfo_.mode;
203     return HDF_SUCCESS;
204 }
205 
UpdatePort(int32_t mode,const sptr<IUsbdSubscriber> & subscriber)206 int32_t UsbdPort::UpdatePort(int32_t mode, const sptr<IUsbdSubscriber> &subscriber)
207 {
208     switch (mode) {
209         case PORT_MODE_HOST:
210             currentPortInfo_.powerRole = POWER_ROLE_SOURCE;
211             currentPortInfo_.dataRole = DATA_ROLE_HOST;
212             currentPortInfo_.mode = PORT_MODE_HOST;
213             break;
214         case PORT_MODE_DEVICE:
215             currentPortInfo_.powerRole = POWER_ROLE_SINK;
216             currentPortInfo_.dataRole = DATA_ROLE_DEVICE;
217             currentPortInfo_.mode = PORT_MODE_DEVICE;
218             break;
219         default:
220             HDF_LOGE("%{public}s invalid mode:%{public}d", __func__, mode);
221             return HDF_FAILURE;
222     }
223 
224     if (subscriber == nullptr) {
225         HDF_LOGE("%{public}s subscriber is nullptr", __func__);
226         return HDF_FAILURE;
227     }
228     subscriber->PortChangedEvent(currentPortInfo_);
229     return HDF_SUCCESS;
230 }
231 } // namespace V1_1
232 } // namespace Usb
233 } // namespace HDI
234 } // namespace OHOS
235