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 "usb_port_manager.h"
17 #include "hisysevent.h"
18 #include "usb_errors.h"
19
20 using namespace OHOS::HiviewDFX;
21 using namespace OHOS::HDI::Usb::V1_0;
22
23 namespace OHOS {
24 namespace USB {
25 const int32_t SUPPORTED_MODES = 3;
26
UsbPortManager()27 UsbPortManager::UsbPortManager()
28 {
29 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::Init start");
30 usbd_ = IUsbInterface::Get();
31 if (usbd_ == nullptr) {
32 USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::Get inteface failed");
33 }
34 }
35
~UsbPortManager()36 UsbPortManager::~UsbPortManager()
37 {
38 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::unInit start");
39 }
40
Init()41 void UsbPortManager::Init()
42 {
43 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::QueryPort start");
44 int32_t ret = QueryPort();
45 if (ret) {
46 USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::QueryPort false");
47 }
48 }
49
GetPorts(std::vector<UsbPort> & ports)50 int32_t UsbPortManager::GetPorts(std::vector<UsbPort> &ports)
51 {
52 std::lock_guard<std::mutex> lock(mutex_);
53 if (portMap_.size() > 0) {
54 for (auto it = portMap_.begin(); it != portMap_.end(); ++it) {
55 ports.push_back(it->second);
56 }
57 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::GetPorts success");
58 return UEC_OK;
59 } else {
60 int32_t ret = QueryPort();
61 if (ret == UEC_OK) {
62 for (auto it = portMap_.begin(); it != portMap_.end(); ++it) {
63 ports.push_back(it->second);
64 }
65 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::QueryPort and GetPorts success");
66 return ret;
67 }
68 }
69 USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::GetPorts false");
70 return UEC_SERVICE_INVALID_VALUE;
71 }
72
GetSupportedModes(int32_t portId,int32_t & supportedModes)73 int32_t UsbPortManager::GetSupportedModes(int32_t portId, int32_t &supportedModes)
74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 auto it = portMap_.find(portId);
77 if (it != portMap_.end()) {
78 supportedModes = it->second.supportedModes;
79 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::GetSupportedModes success");
80 return UEC_OK;
81 }
82 USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::GetSupportedModes false");
83 return UEC_SERVICE_INVALID_VALUE;
84 }
85
QueryPort()86 int32_t UsbPortManager::QueryPort()
87 {
88 int32_t portId = 0;
89 int32_t powerRole = 0;
90 int32_t dataRole = 0;
91 int32_t mode = 0;
92
93 if (usbd_ == nullptr) {
94 USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::usbd_ is nullptr");
95 return UEC_SERVICE_INVALID_VALUE;
96 }
97 int32_t ret = usbd_->QueryPort(portId, powerRole, dataRole, mode);
98 USB_HILOGI(MODULE_USB_SERVICE, "portId:%{public}d powerRole:%{public}d dataRole:%{public}d mode:%{public}d ",
99 portId, powerRole, dataRole, mode);
100 if (ret) {
101 USB_HILOGE(MODULE_USB_SERVICE, "Get().queryPorts failed");
102 return ret;
103 }
104 UsbPortStatus usbPortStatus;
105 UsbPort usbPort;
106 usbPortStatus.currentMode = mode;
107 usbPortStatus.currentDataRole = dataRole;
108 usbPortStatus.currentPowerRole = powerRole;
109 usbPort.id = portId;
110 usbPort.supportedModes = SUPPORTED_MODES;
111 usbPort.usbPortStatus = usbPortStatus;
112 AddPort(usbPort);
113 return ret;
114 }
115
UpdatePort(int32_t portId,int32_t powerRole,int32_t dataRole,int32_t mode)116 void UsbPortManager::UpdatePort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode)
117 {
118 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::updatePort run");
119 std::lock_guard<std::mutex> lock(mutex_);
120 auto it = portMap_.find(portId);
121 if (it != portMap_.end()) {
122 if (it->second.id == portId) {
123 ReportPortRoleChangeSysEvent(it->second.usbPortStatus.currentPowerRole, powerRole,
124 it->second.usbPortStatus.currentDataRole, dataRole);
125 it->second.usbPortStatus.currentPowerRole = powerRole;
126 it->second.usbPortStatus.currentDataRole = dataRole;
127 it->second.usbPortStatus.currentMode = mode;
128 USB_HILOGI(MODULE_USB_SERVICE, "UsbPortManager::updatePort seccess");
129 return;
130 }
131 }
132 USB_HILOGE(MODULE_USB_SERVICE, "updatePort false");
133 }
134
AddPort(UsbPort & port)135 void UsbPortManager::AddPort(UsbPort &port)
136 {
137 USB_HILOGI(MODULE_USB_SERVICE, "addPort run");
138 auto res = portMap_.insert(std::map<int32_t, UsbPort>::value_type(port.id, port));
139 if (!res.second) {
140 USB_HILOGW(MODULE_USB_SERVICE, "addPort port id duplicated");
141 return;
142 }
143 USB_HILOGI(MODULE_USB_SERVICE, "addPort successed");
144 }
145
RemovePort(int32_t portId)146 void UsbPortManager::RemovePort(int32_t portId)
147 {
148 USB_HILOGI(MODULE_USB_SERVICE, "removePort run");
149 std::lock_guard<std::mutex> lock(mutex_);
150 size_t num = portMap_.erase(portId);
151 if (num == 0) {
152 USB_HILOGW(MODULE_USB_SERVICE, "removePort false");
153 return;
154 }
155 USB_HILOGI(MODULE_USB_SERVICE, "removePort seccess");
156 }
157
Dump(int fd,const std::string & args)158 bool UsbPortManager::Dump(int fd, const std::string &args)
159 {
160 if (args.compare("-a") != 0) {
161 dprintf(fd, "args is not -a\n");
162 return false;
163 }
164
165 dprintf(fd, "Usb Port device status:\n");
166 std::vector<UsbPort> ports;
167 if (GetPorts(ports) != UEC_OK) {
168 dprintf(fd, "UsbPortManager::GetPorts failed, port is empty\n");
169 return UEC_SERVICE_INVALID_VALUE;
170 }
171
172 for (size_t i = 0; i < ports.size(); ++i) {
173 dprintf(fd, "id: %d | supportedModes: %d | currentMode: %d | currentPowerRole: %d | currentDataRole: %d\n",
174 ports[i].id, ports[i].supportedModes, ports[i].usbPortStatus.currentMode,
175 ports[i].usbPortStatus.currentPowerRole, ports[i].usbPortStatus.currentDataRole);
176 }
177 return true;
178 }
179
ReportPortRoleChangeSysEvent(int32_t currentPowerRole,int32_t updatePowerRole,int32_t currentDataRole,int32_t updateDataRole)180 void UsbPortManager::ReportPortRoleChangeSysEvent(
181 int32_t currentPowerRole, int32_t updatePowerRole, int32_t currentDataRole, int32_t updateDataRole)
182 {
183 USB_HILOGI(MODULE_USB_SERVICE, "Port switch points are as follows:");
184 HiSysEvent::Write("USB", "USB_PORT_ROLE_CHANGED", HiSysEvent::EventType::BEHAVIOR, "CURRENT_POWERROLE",
185 currentPowerRole, "UPDATE_POWERROLE", updatePowerRole, "CURRENT_DATAROLE", currentDataRole, "UPDATE_DATAROLE",
186 updateDataRole);
187 }
188 } // namespace USB
189 } // namespace OHOS
190