• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_device_manager.h"
17 #include <hdf_base.h>
18 #include "common_event_data.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "hisysevent.h"
22 #include "usb_errors.h"
23 #include "usb_srv_support.h"
24 #include "usbd_type.h"
25 
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::EventFwk;
28 using namespace OHOS::HiviewDFX;
29 using namespace OHOS::HDI::Usb::V1_0;
30 
31 namespace OHOS {
32 namespace USB {
33 constexpr int32_t PARAM_COUNT_TWO = 2;
34 constexpr int32_t PARAM_COUNT_THR = 3;
35 constexpr uint32_t CMD_INDEX = 1;
36 constexpr uint32_t PARAM_INDEX = 2;
37 const std::map<std::string_view, uint32_t> UsbDeviceManager::FUNCTION_MAPPING_N2C = {
38     {UsbSrvSupport::FUNCTION_NAME_NONE, UsbSrvSupport::FUNCTION_NONE},
39     {UsbSrvSupport::FUNCTION_NAME_ACM, UsbSrvSupport::FUNCTION_ACM},
40     {UsbSrvSupport::FUNCTION_NAME_ECM, UsbSrvSupport::FUNCTION_ECM},
41     {UsbSrvSupport::FUNCTION_NAME_HDC, UsbSrvSupport::FUNCTION_HDC},
42     {UsbSrvSupport::FUNCTION_NAME_MTP, UsbSrvSupport::FUNCTION_MTP},
43     {UsbSrvSupport::FUNCTION_NAME_PTP, UsbSrvSupport::FUNCTION_PTP},
44     {UsbSrvSupport::FUNCTION_NAME_RNDIS, UsbSrvSupport::FUNCTION_RNDIS},
45     {UsbSrvSupport::FUNCTION_NAME_STORAGE, UsbSrvSupport::FUNCTION_STORAGE},
46 };
47 
UsbDeviceManager()48 UsbDeviceManager::UsbDeviceManager()
49 {
50     USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceManager::Init start");
51     usbd_ = IUsbInterface::Get();
52     if (usbd_ == nullptr) {
53         USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager::Get inteface failed");
54     }
55 }
56 
Init()57 int32_t UsbDeviceManager::Init()
58 {
59     std::shared_ptr<UsbFunctionSwitchWindow> window_ = UsbFunctionSwitchWindow::GetInstance();
60     int32_t ret = window_->Init();
61     if (ret != UEC_OK) {
62         USB_HILOGE(MODULE_USB_SERVICE, "Init usb function switch window failed");
63     }
64     return ret;
65 }
66 
SetUsbd(const sptr<IUsbInterface> & usbd)67 int32_t UsbDeviceManager::SetUsbd(const sptr<IUsbInterface> &usbd)
68 {
69     if (usbd == nullptr) {
70         USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager usbd is nullptr");
71         return UEC_SERVICE_INVALID_VALUE;
72     }
73     usbd_ = usbd;
74     return UEC_OK;
75 }
76 
AreSettableFunctions(int32_t funcs)77 bool UsbDeviceManager::AreSettableFunctions(int32_t funcs)
78 {
79     return static_cast<uint32_t>(funcs) == UsbSrvSupport::FUNCTION_NONE ||
80         ((~functionSettable_ & static_cast<uint32_t>(funcs)) == 0);
81 }
82 
ConvertFromString(std::string_view strFun)83 uint32_t UsbDeviceManager::ConvertFromString(std::string_view strFun)
84 {
85     if (strFun == UsbSrvSupport::FUNCTION_NAME_NONE) {
86         return UsbSrvSupport::FUNCTION_NONE;
87     }
88 
89     size_t len = strFun.length();
90     if (len == 0) {
91         return UEC_SERVICE_INVALID_VALUE;
92     }
93 
94     std::vector<std::string_view> vModeStr;
95     size_t pos = 0;
96     while (pos < len) {
97         size_t findPos = strFun.find(",", pos);
98         if (findPos == strFun.npos) {
99             vModeStr.push_back(strFun.substr(pos, len - pos));
100             break;
101         }
102         vModeStr.push_back(strFun.substr(pos, findPos - pos));
103         pos = findPos + 1;
104     }
105 
106     uint32_t ret = 0;
107     for (auto &&item : vModeStr) {
108         auto it = FUNCTION_MAPPING_N2C.find(item);
109         if (it != FUNCTION_MAPPING_N2C.end()) {
110             ret |= it->second;
111         } else {
112             USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager::ConvertFromString Invalid argument of usb function");
113             return UEC_SERVICE_INVALID_VALUE;
114         }
115     }
116 
117     return ret;
118 }
119 
ConvertToString(uint32_t function)120 std::string UsbDeviceManager::ConvertToString(uint32_t function)
121 {
122     std::string stream;
123     if (function <= UsbSrvSupport::FUNCTION_NONE || function > functionSettable_) {
124         stream = std::string {UsbSrvSupport::FUNCTION_NAME_NONE};
125         return stream;
126     }
127     bool flag = false;
128     for (auto it = FUNCTION_MAPPING_N2C.begin(); it != FUNCTION_MAPPING_N2C.end(); ++it) {
129         if ((function & it->second) != 0) {
130             if (flag) {
131                 stream += ",";
132             }
133             stream += std::string {it->first};
134             flag = true;
135         }
136     }
137     USB_HILOGI(MODULE_USB_SERVICE, "UsbDeviceManager::ConvertToString success");
138     return stream;
139 }
140 
UpdateFunctions(int32_t func)141 void UsbDeviceManager::UpdateFunctions(int32_t func)
142 {
143     ReportFuncChangeSysEvent(currentFunctions_, func);
144     currentFunctions_ = func;
145 }
146 
GetCurrentFunctions()147 int32_t UsbDeviceManager::GetCurrentFunctions()
148 {
149     return currentFunctions_;
150 }
151 
HandleEvent(int32_t status)152 void UsbDeviceManager::HandleEvent(int32_t status)
153 {
154     if (usbd_ == nullptr) {
155         USB_HILOGE(MODULE_USB_SERVICE, "UsbDeviceManager::usbd_ is nullptr");
156         return;
157     }
158     switch (status) {
159         case ACT_UPDEVICE: {
160             connected_ = true;
161             usbd_->GetCurrentFunctions(currentFunctions_);
162             break;
163         }
164         case ACT_DOWNDEVICE: {
165             connected_ = false;
166             break;
167         }
168         default:
169             USB_HILOGE(MODULE_USB_SERVICE, "invalid status %{public}d", status);
170             return;
171     }
172 
173     Want want;
174     want.SetAction(CommonEventSupport::COMMON_EVENT_USB_STATE);
175 
176     want.SetParam(std::string {UsbSrvSupport::CONNECTED}, connected_);
177     uint32_t remainderFunc = static_cast<uint32_t>(currentFunctions_);
178     // start from bit 1
179     uint32_t bit = 1;
180     while (remainderFunc != 0) {
181         if (remainderFunc & bit) {
182             want.SetParam(ConvertToString(bit), true);
183             // set current bit to zero
184             remainderFunc &= ~bit;
185         }
186         // 1 means to next bit
187         bit = bit << 1;
188     }
189 
190     CommonEventData data(want);
191     CommonEventPublishInfo publishInfo;
192     USB_HILOGI(MODULE_SERVICE, "send COMMON_EVENT_USB_STATE broadcast connected:%{public}d, "
193         "currentFunctions:%{public}d", connected_, currentFunctions_);
194     CommonEventManager::PublishCommonEvent(data, publishInfo);
195     ReportDevicePlugSysEvent(currentFunctions_, connected_);
196     ProcessFunctionSwitchWindow(status);
197 }
198 
ProcessFunctionSwitchWindow(int32_t status)199 void UsbDeviceManager::ProcessFunctionSwitchWindow(int32_t status)
200 {
201     std::shared_ptr<UsbFunctionSwitchWindow> window_ = UsbFunctionSwitchWindow::GetInstance();
202     if (status == ACT_UPDEVICE) {
203         USB_HILOGD(MODULE_USB_SERVICE, "start pop up usb service switch window");
204         if (!window_->PopUpFunctionSwitchWindow()) {
205             USB_HILOGE(MODULE_USB_SERVICE, "start pop up usb service switch window failed");
206         }
207     } else if (status == ACT_DOWNDEVICE) {
208         USB_HILOGD(MODULE_USB_SERVICE, "start dismiss usb service switch window");
209         if (!window_->DismissFunctionSwitchWindow()) {
210             USB_HILOGE(MODULE_USB_SERVICE, "start dismiss usb service switch window failed");
211         }
212         uint32_t targetFunction = static_cast<uint32_t>(currentFunctions_);
213         targetFunction &= ~(UsbSrvSupport::FUNCTION_MTP | UsbSrvSupport::FUNCTION_PTP);
214         int32_t ret = usbd_->SetCurrentFunctions(targetFunction);
215         if (ret != UEC_OK) {
216             USB_HILOGE(MODULE_USB_SERVICE, "restore function from %{public}d to %{public}d failed: %{public}d",
217                 currentFunctions_, targetFunction, ret);
218         }
219     }
220 }
221 
GetDumpHelp(int32_t fd)222 void UsbDeviceManager::GetDumpHelp(int32_t fd)
223 {
224     dprintf(fd, "========= dump the all device function =========\n");
225     dprintf(fd, "usb_device -a:    Query all function\n");
226     dprintf(fd, "usb_device -f Q:  Query Current function\n");
227     dprintf(fd, "usb_device -f 0:  Switch to function:none\n");
228     dprintf(fd, "usb_device -f 1:  Switch to function:acm\n");
229     dprintf(fd, "usb_device -f 2:  Switch to function:ecm\n");
230     dprintf(fd, "usb_device -f 3:  Switch to function:acm&ecm\n");
231     dprintf(fd, "usb_device -f 4:  Switch to function:hdc\n");
232     dprintf(fd, "usb_device -f 5:  Switch to function:acm&hdc\n");
233     dprintf(fd, "usb_device -f 6:  Switch to function:ecm&hdc\n");
234     dprintf(fd, "usb_device -f 32: Switch to function:rndis\n");
235     dprintf(fd, "usb_device -f 512:Switch to function:storage\n");
236     dprintf(fd, "usb_device -f 36: Switch to function:rndis&hdc\n");
237     dprintf(fd, "usb_device -f 516:Switch to function:storage&hdc\n");
238     dprintf(fd, "------------------------------------------------\n");
239 }
240 
DumpGetSupportFunc(int32_t fd)241 void UsbDeviceManager::DumpGetSupportFunc(int32_t fd)
242 {
243     dprintf(fd, "Usb Device function list info:\n");
244     dprintf(fd, "current function: %s\n", ConvertToString(currentFunctions_).c_str());
245     dprintf(fd, "supported functions list: %s\n", ConvertToString(functionSettable_).c_str());
246 }
247 
DumpSetFunc(int32_t fd,const std::string & args)248 void UsbDeviceManager::DumpSetFunc(int32_t fd, const std::string &args)
249 {
250     int32_t currentFunction;
251     int32_t ret;
252     if (args.compare("Q") == 0) {
253         ret = usbd_->GetCurrentFunctions(currentFunction);
254         if (ret != UEC_OK) {
255             dprintf(fd, "GetCurrentFunctions failed: %d\n", __LINE__);
256             return;
257         }
258         dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str());
259         return;
260     }
261 
262     int32_t mode = stoi(args);
263     ret = usbd_->SetCurrentFunctions(mode);
264     if (ret != UEC_OK) {
265         dprintf(fd, "SetCurrentFunctions failed");
266         return;
267     }
268     ret = usbd_->GetCurrentFunctions(currentFunction);
269     if (ret != UEC_OK) {
270         dprintf(fd, "GetCurrentFunctions failed: %d\n", __LINE__);
271         return;
272     }
273 
274     dprintf(fd, "current function: %s\n", ConvertToString(currentFunction).c_str());
275 }
276 
Dump(int32_t fd,const std::vector<std::string> & args)277 void UsbDeviceManager::Dump(int32_t fd, const std::vector<std::string> &args)
278 {
279     if (args.size() < PARAM_COUNT_TWO || args.size() > PARAM_COUNT_THR) {
280         GetDumpHelp(fd);
281         return;
282     }
283 
284     if (args[CMD_INDEX] == "-a" && args.size() == PARAM_COUNT_TWO) {
285         DumpGetSupportFunc(fd);
286     } else if (args[CMD_INDEX] == "-f" && args.size() == PARAM_COUNT_THR) {
287         DumpSetFunc(fd, args[PARAM_INDEX]);
288     } else {
289         dprintf(fd, "func param error, please enter again\n");
290         GetDumpHelp(fd);
291     }
292 }
293 
ReportFuncChangeSysEvent(int32_t currentFunctions,int32_t updateFunctions)294 void UsbDeviceManager::ReportFuncChangeSysEvent(int32_t currentFunctions, int32_t updateFunctions)
295 {
296     USB_HILOGI(MODULE_USB_SERVICE, "Device function Indicates the switch point information:");
297     HiSysEventWrite(HiSysEvent::Domain::USB, "FUNCTION_CHANGED",
298         HiSysEvent::EventType::BEHAVIOR, "CURRENT_FUNCTION",
299         currentFunctions_, "UPDATE_FUNCTION", updateFunctions);
300 }
301 
ReportDevicePlugSysEvent(int32_t currentFunctions,bool connected)302 void UsbDeviceManager::ReportDevicePlugSysEvent(int32_t currentFunctions, bool connected)
303 {
304     USB_HILOGI(MODULE_USB_SERVICE, "Device mode Indicates the insertion and removal information:");
305     HiSysEventWrite(HiSysEvent::Domain::USB, "PLUG_IN_OUT_DEVICE_MODE",
306         HiSysEvent::EventType::BEHAVIOR, "CURRENT_FUNCTIONS",
307         currentFunctions, "CONNECTED", connected);
308 }
IsGadgetConnected(void)309 bool UsbDeviceManager::IsGadgetConnected(void)
310 {
311     return connected_;
312 }
313 } // namespace USB
314 } // namespace OHOS
315