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