1 /*
2 * Copyright (c) 2021-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 "usb_service.h"
17
18 #include <ipc_skeleton.h>
19 #include <unistd.h>
20
21 #include <cstdio>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25
26 #include "bundle_mgr_interface.h"
27 #include "bundle_mgr_proxy.h"
28 #include "file_ex.h"
29 #include "if_system_ability_manager.h"
30 #include "iservice_registry.h"
31 #include "iusb_srv.h"
32 #include "securec.h"
33 #include "system_ability_definition.h"
34 #include "usb_common.h"
35 #include "usb_descriptor_parser.h"
36 #include "usb_errors.h"
37 #include "usb_port_manager.h"
38 #include "usb_right_manager.h"
39 #include "usbd_bulkcallback_impl.h"
40
41 using OHOS::sptr;
42 using namespace OHOS::HDI::Usb::V1_0;
43
44 namespace OHOS {
45 namespace USB {
46 namespace {
47 constexpr const char *USB_SERVICE_NAME = "UsbService";
48 constexpr int32_t COMMEVENT_REGISTER_RETRY_TIMES = 10;
49 constexpr int32_t COMMEVENT_REGISTER_WAIT_DELAY_US = 20000;
50 constexpr uint32_t CURSOR_INIT = 18;
51 constexpr int32_t DESCRIPTOR_TYPE_STRING = 3;
52 constexpr int32_t DESCRIPTOR_VALUE_START_OFFSET = 2;
53 constexpr int32_t HALF = 2;
54 constexpr int32_t BIT_SHIFT_4 = 4;
55 constexpr int32_t BIT_HIGH_4 = 0xF0;
56 constexpr int32_t BIT_LOW_4 = 0x0F;
57 constexpr int32_t SERVICE_STARTUP_MAX_TIME = 30;
58 } // namespace
59
60 auto pms = DelayedSpSingleton<UsbService>::GetInstance();
61 const bool G_REGISTER_RESULT =
62 SystemAbility::MakeAndRegisterAbility(DelayedSpSingleton<UsbService>::GetInstance().GetRefPtr());
63
UsbService()64 UsbService::UsbService() : SystemAbility(USB_SYSTEM_ABILITY_ID, true)
65 {
66 usbHostManger_ = std::make_shared<UsbHostManager>(nullptr);
67 usbRightManager_ = std::make_shared<UsbRightManager>();
68 usbPortManager_ = std::make_shared<UsbPortManager>();
69 usbDeviceManager_ = std::make_shared<UsbDeviceManager>();
70 usbd_ = IUsbInterface::Get();
71 if (usbd_ == nullptr) {
72 USB_HILOGE(MODULE_USB_SERVICE, "IUsbInterface::Get inteface failed");
73 }
74 }
~UsbService()75 UsbService::~UsbService() {}
76
SystemAbilityStatusChangeListener(sptr<UsbServiceSubscriber> usbdSubscriber)77 UsbService::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
78 sptr<UsbServiceSubscriber> usbdSubscriber)
79 : usbdSubscriber_(usbdSubscriber) {}
80
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)81 void UsbService::SystemAbilityStatusChangeListener::OnAddSystemAbility(
82 int32_t systemAbilityId, const std::string &deviceId)
83 {
84 USB_HILOGI(MODULE_USB_SERVICE, "OnAddSystemAbility ID = %{public}d", systemAbilityId);
85 }
86
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)87 void UsbService::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
88 int32_t systemAbilityId, const std::string &deviceId)
89 {
90 USB_HILOGI(MODULE_USB_SERVICE, "OnRemoveSystemAbility ID = %{public}d", systemAbilityId);
91 if (systemAbilityId == USB_SYSTEM_ABILITY_ID) {
92 sptr<IUsbInterface> usbd_ = IUsbInterface::Get();
93 if (usbd_ != nullptr) {
94 usbd_->UnbindUsbdSubscriber(usbdSubscriber_);
95 }
96 exit(0);
97 }
98 }
99
OnStart()100 void UsbService::OnStart()
101 {
102 USB_HILOGI(MODULE_USB_SERVICE, "usb_service OnStart enter");
103 if (ready_) {
104 USB_HILOGE(MODULE_USB_SERVICE, "OnStart is ready, nothing to do");
105 return;
106 }
107
108 if (!(Init())) {
109 USB_HILOGE(MODULE_USB_SERVICE, "OnStart call init fail");
110 return;
111 }
112
113 // wait for the usbd service to start and bind usb service and usbd service
114 int32_t retryTimes = 0;
115 while (retryTimes < SERVICE_STARTUP_MAX_TIME) {
116 if (InitUsbd() != 0) {
117 break;
118 }
119 sleep(1);
120 retryTimes++;
121
122 if (retryTimes == SERVICE_STARTUP_MAX_TIME) {
123 USB_HILOGE(MODULE_USB_SERVICE, "OnStart call initUsbd failed");
124 return;
125 }
126 }
127
128 if (usbPortManager_ == nullptr) {
129 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbPortManager_");
130 return;
131 }
132
133 usbPortManager_->Init();
134 ready_ = true;
135 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
136 sptr<ISystemAbilityStatusChange> status = new (std::nothrow) SystemAbilityStatusChangeListener(usbdSubscriber_);
137 if (samgrProxy == nullptr || status == nullptr) {
138 USB_HILOGE(MODULE_USB_SERVICE, "samgrProxy or SystemAbilityStatusChangeListener is nullptr");
139 return;
140 }
141 int32_t ret = samgrProxy->SubscribeSystemAbility(USB_SYSTEM_ABILITY_ID, status);
142 if (ret != UEC_OK) {
143 USB_HILOGE(MODULE_USB_SERVICE, "SubscribeSystemAbility failed. ret = %{public}d", ret);
144 return;
145 }
146 USB_HILOGE(MODULE_USB_SERVICE, "OnStart and add system ability success");
147 }
148
Init()149 bool UsbService::Init()
150 {
151 USB_HILOGI(MODULE_USB_SERVICE, "usb_service Init enter");
152 if (!eventRunner_) {
153 eventRunner_ = AppExecFwk::EventRunner::Create(USB_SERVICE_NAME);
154 if (eventRunner_ == nullptr) {
155 USB_HILOGE(MODULE_USB_SERVICE, "Init failed due to create EventRunner");
156 return false;
157 }
158 }
159 if (handler_ == nullptr) {
160 handler_ = std::make_shared<UsbServerEventHandler>(eventRunner_, pms);
161
162 if (!Publish(pms)) {
163 USB_HILOGE(MODULE_USB_SERVICE, "OnStart register to system ability manager failed.");
164 return false;
165 }
166 }
167 while (commEventRetryTimes_ <= COMMEVENT_REGISTER_RETRY_TIMES) {
168 if (!IsCommonEventServiceAbilityExist()) {
169 ++commEventRetryTimes_;
170 usleep(COMMEVENT_REGISTER_WAIT_DELAY_US);
171 } else {
172 commEventRetryTimes_ = 0;
173 break;
174 }
175 }
176 USB_HILOGE(MODULE_USB_SERVICE, "Init success");
177 return true;
178 }
179
InitUsbd()180 bool UsbService::InitUsbd()
181 {
182 usbdSubscriber_ = new (std::nothrow) UsbServiceSubscriber();
183 if (usbdSubscriber_ == nullptr) {
184 USB_HILOGE(MODULE_USB_SERVICE, "Init failed\n");
185 return false;
186 }
187
188 if (usbd_ == nullptr) {
189 USB_HILOGE(MODULE_USB_SERVICE, "UsbPortManager::usbd_ is nullptr");
190 return false;
191 }
192 ErrCode ret = usbd_->BindUsbdSubscriber(usbdSubscriber_);
193 USB_HILOGI(MODULE_USB_SERVICE, "entry InitUsbd ret: %{public}d", ret);
194 return SUCCEEDED(ret);
195 }
196
OnStop()197 void UsbService::OnStop()
198 {
199 USB_HILOGI(MODULE_USB_SERVICE, "entry stop service %{public}d", ready_);
200 if (!ready_) {
201 return;
202 }
203 eventRunner_.reset();
204 handler_.reset();
205 ready_ = false;
206
207 if (usbd_ == nullptr) {
208 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
209 return;
210 }
211 usbd_->UnbindUsbdSubscriber(usbdSubscriber_);
212 }
213
IsCommonEventServiceAbilityExist()214 bool UsbService::IsCommonEventServiceAbilityExist()
215 {
216 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
217 if (sm == nullptr) {
218 USB_HILOGE(MODULE_USB_SERVICE,
219 "IsCommonEventServiceAbilityExist Get ISystemAbilityManager "
220 "failed, no SystemAbilityManager");
221 return false;
222 }
223 sptr<IRemoteObject> remote = sm->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
224 if (!remote) {
225 USB_HILOGE(MODULE_USB_SERVICE, "No CesServiceAbility");
226 return false;
227 }
228 return true;
229 }
230
OpenDevice(uint8_t busNum,uint8_t devAddr)231 int32_t UsbService::OpenDevice(uint8_t busNum, uint8_t devAddr)
232 {
233 std::string name = std::to_string(busNum) + "-" + std::to_string(devAddr);
234 if (!UsbService::HasRight(name)) {
235 USB_HILOGE(MODULE_USB_SERVICE, "No permission");
236 return UEC_SERVICE_PERMISSION_DENIED;
237 }
238
239 const UsbDev dev = {busNum, devAddr};
240 if (usbd_ == nullptr) {
241 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
242 return UEC_SERVICE_INVALID_VALUE;
243 }
244 int32_t ret = usbd_->OpenDevice(dev);
245 if (ret != UEC_OK) {
246 USB_HILOGE(MODULE_USB_SERVICE, "OpenDevice failed ret:%{public}d", ret);
247 }
248
249 return ret;
250 }
251
HasRight(std::string deviceName)252 bool UsbService::HasRight(std::string deviceName)
253 {
254 USB_HILOGI(MODULE_USB_SERVICE, "calling usbRightManager HasRight");
255 std::string bundleName;
256 if (!GetBundleName(bundleName)) {
257 USB_HILOGE(MODULE_USB_SERVICE, "HasRight GetBundleName false");
258 return false;
259 }
260 USB_HILOGI(MODULE_USB_SERVICE, "HasRight bundleName = %{public}s", bundleName.c_str());
261
262 if (usbRightManager_ == nullptr) {
263 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
264 return false;
265 }
266 return usbRightManager_->HasRight(deviceName, bundleName);
267 }
268
RequestRight(std::string deviceName)269 int32_t UsbService::RequestRight(std::string deviceName)
270 {
271 USB_HILOGI(MODULE_USB_SERVICE, "calling usbRightManager RequestRight");
272 std::string bundleName;
273 if (!GetBundleName(bundleName)) {
274 USB_HILOGI(MODULE_USB_SERVICE, "RequestRight GetBundleName false");
275 return UEC_SERVICE_INNER_ERR;
276 }
277
278 if (usbRightManager_ == nullptr) {
279 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
280 return UEC_SERVICE_INVALID_VALUE;
281 }
282 USB_HILOGI(MODULE_USB_SERVICE, "RequestRight bundleName = %{public}s", bundleName.c_str());
283 return usbRightManager_->RequestRight(deviceName, bundleName);
284 }
285
RemoveRight(std::string deviceName)286 int32_t UsbService::RemoveRight(std::string deviceName)
287 {
288 if (usbRightManager_ == nullptr) {
289 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
290 return UEC_SERVICE_INVALID_VALUE;
291 }
292
293 std::string bundleName;
294 if (!GetBundleName(bundleName)) {
295 USB_HILOGE(MODULE_USB_SERVICE, "RequestRight GetBundleName false");
296 return UEC_SERVICE_INNER_ERR;
297 }
298
299 if (!usbRightManager_->RemoveDeviceRight(deviceName, bundleName)) {
300 USB_HILOGE(MODULE_USB_SERVICE, "RemoveDeviceRight failed");
301 return UEC_SERVICE_INNER_ERR;
302 }
303 USB_HILOGI(MODULE_USB_SERVICE, "RemoveRight done");
304 return UEC_OK;
305 }
306
GetDevices(std::vector<UsbDevice> & deviceList)307 int32_t UsbService::GetDevices(std::vector<UsbDevice> &deviceList)
308 {
309 std::map<std::string, UsbDevice *> devices;
310
311 if (usbHostManger_ == nullptr) {
312 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbHostManger_");
313 return UEC_SERVICE_INVALID_VALUE;
314 }
315
316 usbHostManger_->GetDevices(devices);
317 USB_HILOGI(MODULE_USB_SERVICE, "list size %{public}zu", devices.size());
318 for (auto it = devices.begin(); it != devices.end(); ++it) {
319 deviceList.push_back(*it->second);
320 }
321 return UEC_OK;
322 }
323
GetCurrentFunctions(int32_t & functions)324 int32_t UsbService::GetCurrentFunctions(int32_t &functions)
325 {
326 if (usbRightManager_ == nullptr) {
327 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
328 return UEC_SERVICE_INVALID_VALUE;
329 }
330 if (!(usbRightManager_->IsSystemHap())) {
331 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
332 return UEC_SERVICE_PERMISSION_DENIED_SYSAPI;
333 }
334 if (usbd_ == nullptr) {
335 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
336 return UEC_SERVICE_INVALID_VALUE;
337 }
338 return usbd_->GetCurrentFunctions(functions);
339 }
340
SetCurrentFunctions(int32_t functions)341 int32_t UsbService::SetCurrentFunctions(int32_t functions)
342 {
343 USB_HILOGI(MODULE_USB_SERVICE, "func = %{public}d", functions);
344 if (usbRightManager_ == nullptr) {
345 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
346 return UEC_SERVICE_INVALID_VALUE;
347 }
348 if (!(usbRightManager_->IsSystemHap())) {
349 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
350 return UEC_SERVICE_PERMISSION_DENIED_SYSAPI;
351 }
352
353 if (usbDeviceManager_ == nullptr) {
354 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbDeviceManager_");
355 return UEC_SERVICE_INVALID_VALUE;
356 }
357 usbDeviceManager_->UpdateFunctions(functions);
358
359 if (usbd_ == nullptr) {
360 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
361 return UEC_SERVICE_INVALID_VALUE;
362 }
363 return usbd_->SetCurrentFunctions(functions);
364 }
365
UsbFunctionsFromString(std::string_view funcs)366 int32_t UsbService::UsbFunctionsFromString(std::string_view funcs)
367 {
368 if (usbRightManager_ == nullptr) {
369 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
370 return UEC_SERVICE_INVALID_VALUE;
371 }
372 if (!(usbRightManager_->IsSystemHap())) {
373 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
374 return UEC_SERVICE_PERMISSION_DENIED_SYSAPI;
375 }
376 USB_HILOGI(MODULE_USB_SERVICE, "calling UsbFunctionsFromString");
377 return UsbDeviceManager::ConvertFromString(funcs);
378 }
379
UsbFunctionsToString(int32_t funcs)380 std::string UsbService::UsbFunctionsToString(int32_t funcs)
381 {
382 if (usbRightManager_ == nullptr) {
383 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
384 return "";
385 }
386 if (!(usbRightManager_->IsSystemHap())) {
387 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
388 return "";
389 }
390 USB_HILOGI(MODULE_USB_SERVICE, "calling UsbFunctionsToString");
391 return UsbDeviceManager::ConvertToString(funcs);
392 }
393
GetPorts(std::vector<UsbPort> & ports)394 int32_t UsbService::GetPorts(std::vector<UsbPort> &ports)
395 {
396 USB_HILOGI(MODULE_USB_SERVICE, "calling usbPortManager getPorts");
397 if (usbRightManager_ == nullptr) {
398 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
399 return UEC_SERVICE_INVALID_VALUE;
400 }
401 if (!(usbRightManager_->IsSystemHap())) {
402 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
403 return UEC_SERVICE_PERMISSION_DENIED_SYSAPI;
404 }
405 if (usbPortManager_ == nullptr) {
406 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbPortManager_");
407 return UEC_SERVICE_INVALID_VALUE;
408 }
409 return usbPortManager_->GetPorts(ports);
410 }
411
GetSupportedModes(int32_t portId,int32_t & supportedModes)412 int32_t UsbService::GetSupportedModes(int32_t portId, int32_t &supportedModes)
413 {
414 USB_HILOGI(MODULE_USB_SERVICE, "calling usbPortManager getSupportedModes");
415 if (usbRightManager_ == nullptr) {
416 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
417 return UEC_SERVICE_INVALID_VALUE;
418 }
419 if (!(usbRightManager_->IsSystemHap())) {
420 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
421 return UEC_SERVICE_PERMISSION_DENIED_SYSAPI;
422 }
423 if (usbPortManager_ == nullptr) {
424 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbPortManager_");
425 return UEC_SERVICE_INVALID_VALUE;
426 }
427 return usbPortManager_->GetSupportedModes(portId, supportedModes);
428 }
429
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)430 int32_t UsbService::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
431 {
432 USB_HILOGI(MODULE_USB_SERVICE, "calling usbd getPorts");
433 if (usbRightManager_ == nullptr) {
434 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
435 return UEC_SERVICE_INVALID_VALUE;
436 }
437 if (!(usbRightManager_->IsSystemHap())) {
438 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
439 return UEC_SERVICE_PERMISSION_DENIED_SYSAPI;
440 }
441 if (usbd_ == nullptr) {
442 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
443 return UEC_SERVICE_INVALID_VALUE;
444 }
445 return usbd_->SetPortRole(portId, powerRole, dataRole);
446 }
447
ClaimInterface(uint8_t busNum,uint8_t devAddr,uint8_t interface,uint8_t force)448 int32_t UsbService::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t force)
449 {
450 const UsbDev dev = {busNum, devAddr};
451 if (usbd_ == nullptr) {
452 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
453 return UEC_SERVICE_INVALID_VALUE;
454 }
455 return usbd_->ClaimInterface(dev, interface, force);
456 }
457
ReleaseInterface(uint8_t busNum,uint8_t devAddr,uint8_t interface)458 int32_t UsbService::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface)
459 {
460 const UsbDev dev = {busNum, devAddr};
461 if (usbd_ == nullptr) {
462 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
463 return UEC_SERVICE_INVALID_VALUE;
464 }
465 return usbd_->ReleaseInterface(dev, interface);
466 }
467
BulkTransferRead(const UsbDev & devInfo,const UsbPipe & pipe,std::vector<uint8_t> & bufferData,int32_t timeOut)468 int32_t UsbService::BulkTransferRead(
469 const UsbDev &devInfo, const UsbPipe &pipe, std::vector<uint8_t> &bufferData, int32_t timeOut)
470 {
471 if (usbd_ == nullptr) {
472 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
473 return UEC_SERVICE_INVALID_VALUE;
474 }
475 int32_t ret = usbd_->BulkTransferRead(devInfo, pipe, timeOut, bufferData);
476 if (ret != UEC_OK) {
477 USB_HILOGE(MODULE_USB_SERVICE, "BulkTransferRead error ret:%{public}d", ret);
478 }
479 return ret;
480 }
481
BulkTransferWrite(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & bufferData,int32_t timeOut)482 int32_t UsbService::BulkTransferWrite(
483 const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData, int32_t timeOut)
484 {
485 if (usbd_ == nullptr) {
486 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
487 return UEC_SERVICE_INVALID_VALUE;
488 }
489 int32_t ret = usbd_->BulkTransferWrite(dev, pipe, timeOut, bufferData);
490 if (ret != UEC_OK) {
491 USB_HILOGE(MODULE_USB_SERVICE, "BulkTransferWrite error ret:%{public}d", ret);
492 }
493 return ret;
494 }
495
ControlTransfer(const UsbDev & dev,const UsbCtrlTransfer & ctrl,std::vector<uint8_t> & bufferData)496 int32_t UsbService::ControlTransfer(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
497 {
498 if (usbd_ == nullptr) {
499 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
500 return UEC_SERVICE_INVALID_VALUE;
501 }
502
503 int32_t ret = UEC_SERVICE_INNER_ERR;
504 if (((uint32_t)ctrl.requestType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
505 ret = usbd_->ControlTransferWrite(dev, ctrl, bufferData);
506 if (ret != UEC_OK) {
507 USB_HILOGE(MODULE_USB_SERVICE, "ControlTransferWrite error ret:%{public}d", ret);
508 }
509 } else {
510 bufferData.clear();
511 ret = usbd_->ControlTransferRead(dev, ctrl, bufferData);
512 if (ret != UEC_OK) {
513 USB_HILOGE(MODULE_USB_SERVICE, "ControlTransferRead error ret:%{public}d", ret);
514 }
515 }
516 return ret;
517 }
518
SetActiveConfig(uint8_t busNum,uint8_t devAddr,uint8_t configIndex)519 int32_t UsbService::SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex)
520 {
521 const UsbDev dev = {busNum, devAddr};
522 if (usbd_ == nullptr) {
523 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
524 return UEC_SERVICE_INVALID_VALUE;
525 }
526 return usbd_->SetConfig(dev, configIndex);
527 }
528
GetActiveConfig(uint8_t busNum,uint8_t devAddr,uint8_t & configIndex)529 int32_t UsbService::GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex)
530 {
531 const UsbDev dev = {busNum, devAddr};
532 if (usbd_ == nullptr) {
533 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
534 return UEC_SERVICE_INVALID_VALUE;
535 }
536 return usbd_->GetConfig(dev, configIndex);
537 }
538
SetInterface(uint8_t busNum,uint8_t devAddr,uint8_t interfaceid,uint8_t altIndex)539 int32_t UsbService::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex)
540 {
541 const UsbDev dev = {busNum, devAddr};
542 if (usbd_ == nullptr) {
543 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
544 return UEC_SERVICE_INVALID_VALUE;
545 }
546 return usbd_->SetInterface(dev, interfaceid, altIndex);
547 }
548
GetRawDescriptor(uint8_t busNum,uint8_t devAddr,std::vector<uint8_t> & bufferData)549 int32_t UsbService::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData)
550 {
551 const UsbDev dev = {busNum, devAddr};
552 if (usbd_ == nullptr) {
553 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
554 return UEC_SERVICE_INVALID_VALUE;
555 }
556 int32_t ret = usbd_->GetRawDescriptor(dev, bufferData);
557 if (ret != UEC_OK) {
558 USB_HILOGE(MODULE_USB_SERVICE, "error ret:%{public}d", ret);
559 }
560 return ret;
561 }
562
GetFileDescriptor(uint8_t busNum,uint8_t devAddr,int32_t & fd)563 int32_t UsbService::GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd)
564 {
565 const UsbDev dev = {busNum, devAddr};
566 if (usbd_ == nullptr) {
567 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
568 return UEC_SERVICE_INVALID_VALUE;
569 }
570 int32_t ret = usbd_->GetFileDescriptor(dev, fd);
571 if (ret != UEC_OK) {
572 USB_HILOGE(MODULE_USB_SERVICE, "error ret:%{public}d", ret);
573 }
574 return ret;
575 }
576
RequestQueue(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & clientData,const std::vector<uint8_t> & bufferData)577 int32_t UsbService::RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
578 const std::vector<uint8_t> &bufferData)
579 {
580 if (usbd_ == nullptr) {
581 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
582 return UEC_SERVICE_INVALID_VALUE;
583 }
584 int32_t ret = usbd_->RequestQueue(dev, pipe, clientData, bufferData);
585 if (ret != UEC_OK) {
586 USB_HILOGE(MODULE_USB_SERVICE, "error ret:%{public}d", ret);
587 }
588 return ret;
589 }
590
RequestWait(const UsbDev & dev,int32_t timeOut,std::vector<uint8_t> & clientData,std::vector<uint8_t> & bufferData)591 int32_t UsbService::RequestWait(
592 const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData, std::vector<uint8_t> &bufferData)
593 {
594 if (usbd_ == nullptr) {
595 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
596 return UEC_SERVICE_INVALID_VALUE;
597 }
598 int32_t ret = usbd_->RequestWait(dev, clientData, bufferData, timeOut);
599 if (ret != UEC_OK) {
600 USB_HILOGE(MODULE_USB_SERVICE, "error ret:%{public}d", ret);
601 }
602 return ret;
603 }
604
RequestCancel(uint8_t busNum,uint8_t devAddr,uint8_t interfaceId,uint8_t endpointId)605 int32_t UsbService::RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointId)
606 {
607 const UsbDev dev = {busNum, devAddr};
608 const UsbPipe pipe = {interfaceId, endpointId};
609 if (usbd_ == nullptr) {
610 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
611 return UEC_SERVICE_INVALID_VALUE;
612 }
613 return usbd_->RequestCancel(dev, pipe);
614 }
615
Close(uint8_t busNum,uint8_t devAddr)616 int32_t UsbService::Close(uint8_t busNum, uint8_t devAddr)
617 {
618 const UsbDev dev = {busNum, devAddr};
619 if (usbd_ == nullptr) {
620 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
621 return UEC_SERVICE_INVALID_VALUE;
622 }
623 return usbd_->CloseDevice(dev);
624 }
625
GetDevStringValFromIdx(uint8_t busNum,uint8_t devAddr,uint8_t idx)626 std::string UsbService::GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, uint8_t idx)
627 {
628 const UsbDev dev = {busNum, devAddr};
629 std::vector<uint8_t> strV;
630 std::string strDesc = " ";
631
632 if (idx == 0) {
633 return strDesc;
634 }
635
636 if (usbd_ == nullptr) {
637 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
638 return nullptr;
639 }
640 int32_t ret = usbd_->GetStringDescriptor(dev, idx, strV);
641 if (ret != UEC_OK) {
642 USB_HILOGE(MODULE_USB_SERVICE, "get string[%{public}hhu] failed ret:%{public}d", idx, ret);
643 return strDesc;
644 }
645 uint32_t length = strV.size();
646 if ((length < DESCRIPTOR_VALUE_START_OFFSET) || (strV[1] != DESCRIPTOR_TYPE_STRING)) {
647 USB_HILOGI(MODULE_USB_SERVICE, "type or length error, len:%{public}u", length);
648 return strDesc;
649 }
650
651 uint16_t *tbuf = new (std::nothrow) uint16_t[length + 1]();
652 if (tbuf == nullptr) {
653 USB_HILOGI(MODULE_USB_SERVICE, "new failed\n");
654 return strDesc;
655 }
656
657 for (uint32_t i = 0; i < length - DESCRIPTOR_VALUE_START_OFFSET; ++i) {
658 tbuf[i] = strV[i + DESCRIPTOR_VALUE_START_OFFSET];
659 }
660 std::wstring wstr(reinterpret_cast<wchar_t *>(tbuf), (length - DESCRIPTOR_VALUE_START_OFFSET) / HALF);
661 strDesc = std::string(wstr.begin(), wstr.end());
662 USB_HILOGI(MODULE_USB_SERVICE, "getString idx:%{public}d String:%{public}s length:%{public}d", idx, strDesc.c_str(),
663 length);
664 delete[] tbuf;
665 return strDesc;
666 }
667
BcdToString(const std::vector<uint8_t> & bcd)668 static std::string BcdToString(const std::vector<uint8_t> &bcd)
669 {
670 std::string tstr;
671 for (uint32_t i = 0; i < bcd.size(); ++i) {
672 tstr += std::to_string((bcd[i] & BIT_HIGH_4) >> BIT_SHIFT_4);
673 tstr += std::to_string((bcd[i] & BIT_LOW_4));
674 }
675 return tstr;
676 }
677
FillDevStrings(UsbDevice & dev)678 int32_t UsbService::FillDevStrings(UsbDevice &dev)
679 {
680 uint8_t busNum;
681 uint8_t devAddr;
682 uint8_t offsetValue = 8;
683
684 busNum = dev.GetBusNum();
685 devAddr = dev.GetDevAddr();
686 uint16_t bcdDevice = dev.GetbcdDevice();
687 const std::vector<uint8_t> bcdData {(bcdDevice & 0xff), ((bcdDevice >> offsetValue) & 0xff)};
688 dev.SetVersion(BcdToString(bcdData));
689 dev.SetManufacturerName(GetDevStringValFromIdx(busNum, devAddr, dev.GetiManufacturer()));
690 dev.SetProductName(GetDevStringValFromIdx(busNum, devAddr, dev.GetiProduct()));
691 dev.SetmSerial(GetDevStringValFromIdx(busNum, devAddr, dev.GetiSerialNumber()));
692 USB_HILOGI(MODULE_USB_SERVICE,
693 "iSerial:%{public}d mSerial:%{public}s Manufactur:%{public}s product:%{public}s "
694 "version:%{public}s",
695 dev.GetiSerialNumber(), dev.GetmSerial().c_str(), dev.GetManufacturerName().c_str(),
696 dev.GetProductName().c_str(), dev.GetVersion().c_str());
697
698 std::vector<USBConfig> configs;
699 configs = dev.GetConfigs();
700 for (auto it = configs.begin(); it != configs.end(); ++it) {
701 it->SetName(GetDevStringValFromIdx(busNum, devAddr, it->GetiConfiguration()));
702 USB_HILOGI(MODULE_USB_SERVICE, "Config:%{public}d %{public}s", it->GetiConfiguration(), it->GetName().c_str());
703 std::vector<UsbInterface> interfaces = it->GetInterfaces();
704 for (auto itIF = interfaces.begin(); itIF != interfaces.end(); ++itIF) {
705 itIF->SetName(GetDevStringValFromIdx(busNum, devAddr, itIF->GetiInterface()));
706 USB_HILOGI(MODULE_USB_SERVICE, "interface:%{public}hhu %{public}s", itIF->GetiInterface(),
707 itIF->GetName().c_str());
708 }
709 it->SetInterfaces(interfaces);
710 }
711 dev.SetConfigs(configs);
712
713 return UEC_OK;
714 }
715
GetDeviceInfoDescriptor(const UsbDev & uDev,std::vector<uint8_t> & descriptor,UsbDevice & dev)716 int32_t UsbService::GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector<uint8_t> &descriptor, UsbDevice &dev)
717 {
718 if (usbd_ == nullptr) {
719 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
720 return UEC_SERVICE_INVALID_VALUE;
721 }
722 int32_t ret = usbd_->GetRawDescriptor(uDev, descriptor);
723 if (ret != UEC_OK) {
724 usbd_->CloseDevice(uDev);
725 USB_HILOGE(MODULE_USB_SERVICE, "GetRawDescriptor failed ret=%{public}d busNum:%{public}d devAddr:%{public}d",
726 ret, uDev.busNum, uDev.devAddr);
727 return ret;
728 }
729 uint8_t *buffer = descriptor.data();
730 uint32_t length = descriptor.size();
731 if ((!buffer) || (length == 0)) {
732 USB_HILOGE(MODULE_USB_SERVICE, "GetRawDescriptor failed len=%{public}d busNum:%{public}d devAddr:%{public}d",
733 length, uDev.busNum, uDev.devAddr);
734 return UEC_SERVICE_INVALID_VALUE;
735 }
736 dev.SetBusNum(uDev.busNum);
737 dev.SetDevAddr(uDev.devAddr);
738 dev.SetName(std::to_string(uDev.busNum) + "-" + std::to_string(uDev.devAddr));
739
740 ret = UsbDescriptorParser::ParseDeviceDescriptor(buffer, length, dev);
741 if (ret != UEC_OK) {
742 usbd_->CloseDevice(uDev);
743 USB_HILOGE(MODULE_USB_SERVICE, "ParseDeviceDescriptor failed ret=%{public}d", ret);
744 return ret;
745 }
746 return ret;
747 }
748
GetConfigDescriptor(UsbDevice & dev,std::vector<uint8_t> & descriptor)749 int32_t UsbService::GetConfigDescriptor(UsbDevice &dev, std::vector<uint8_t> &descriptor)
750 {
751 std::vector<USBConfig> configs;
752 uint8_t *buffer = descriptor.data();
753 uint32_t length = descriptor.size();
754 uint32_t cursor = CURSOR_INIT;
755 int32_t ret = UEC_OK;
756 for (uint8_t i = 0; i < dev.GetDescConfigCount(); ++i) {
757 if (length <= cursor) {
758 USB_HILOGE(MODULE_USB_SERVICE, "GetConfigDescriptor[%{public}d] length=%{public}d", i, length);
759 break;
760 }
761 USB_HILOGI(MODULE_USB_SERVICE, "GetConfigDescriptor length=%{public}d", length);
762 uint32_t configCursor = 0;
763 USBConfig config;
764 ret = UsbDescriptorParser::ParseConfigDescriptor(buffer + cursor, length - cursor, configCursor, config);
765 if (ret != UEC_OK) {
766 USB_HILOGE(MODULE_USB_SERVICE, "ParseConfigDescriptor failed ret=%{public}d", ret);
767 return ret;
768 }
769 cursor += configCursor;
770 configs.push_back(config);
771 }
772 dev.SetConfigs(configs);
773 ret = FillDevStrings(dev);
774 USB_HILOGI(MODULE_USB_SERVICE, "FillDevStrings ret=%{public}d", ret);
775 return ret;
776 }
777
GetDeviceInfo(uint8_t busNum,uint8_t devAddr,UsbDevice & dev)778 int32_t UsbService::GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &dev)
779 {
780 USB_HILOGI(MODULE_USB_SERVICE, "busNum:%{public}d devAddr:%{public}d", busNum, devAddr);
781 const UsbDev uDev = {busNum, devAddr};
782 std::vector<uint8_t> descriptor;
783
784 if (usbd_ == nullptr) {
785 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
786 return UEC_SERVICE_INVALID_VALUE;
787 }
788 int32_t ret = usbd_->OpenDevice(uDev);
789 if (ret != UEC_OK) {
790 USB_HILOGE(MODULE_USB_SERVICE, "OpenDevice failed ret=%{public}d", ret);
791 return ret;
792 }
793
794 ret = GetDeviceInfoDescriptor(uDev, descriptor, dev);
795 if (ret != UEC_OK) {
796 USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceInfoDescriptor ret=%{public}d", ret);
797 }
798 ret = GetConfigDescriptor(dev, descriptor);
799 if (ret != UEC_OK) {
800 USB_HILOGE(MODULE_USB_SERVICE, "GetConfigDescriptor ret=%{public}d", ret);
801 return ret;
802 }
803
804 usbd_->CloseDevice(uDev);
805 USB_HILOGI(MODULE_USB_SERVICE, "CloseDevice=%{public}s", dev.ToString().c_str());
806
807 return UEC_OK;
808 }
809
AddDevice(uint8_t busNum,uint8_t devAddr)810 bool UsbService::AddDevice(uint8_t busNum, uint8_t devAddr)
811 {
812 UsbDevice *devInfo = new (std::nothrow) UsbDevice();
813 if (devInfo == nullptr) {
814 USB_HILOGI(MODULE_USB_SERVICE, "new failed");
815 return false;
816 }
817
818 errno_t retSafe = memset_s(devInfo, sizeof(UsbDevice), 0, sizeof(UsbDevice));
819 if (retSafe != EOK) {
820 USB_HILOGI(MODULE_USB_SERVICE, "memset_s failed");
821 return false;
822 }
823
824 int32_t ret = GetDeviceInfo(busNum, devAddr, *devInfo);
825 USB_HILOGI(MODULE_USB_SERVICE, "GetDeviceInfo ret=%{public}d", ret);
826 if (ret != UEC_OK) {
827 delete devInfo;
828 return false;
829 }
830
831 if (usbHostManger_ == nullptr) {
832 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbHostManger_");
833 return false;
834 }
835
836 usbHostManger_->AddDevice(devInfo);
837 return true;
838 }
839
DelDevice(uint8_t busNum,uint8_t devAddr)840 bool UsbService::DelDevice(uint8_t busNum, uint8_t devAddr)
841 {
842 USB_HILOGI(MODULE_USBD, "entry");
843 int32_t ret = Close(busNum, devAddr);
844 if (ret != UEC_OK) {
845 USB_HILOGE(MODULE_USBD, "Close device failed width ret = %{public}d", ret);
846 }
847
848 if (usbHostManger_ == nullptr || usbRightManager_ == nullptr) {
849 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbHostManger_ or usbRightManager_");
850 return false;
851 }
852
853 if (!usbRightManager_->RemoveDeviceAllRight(std::to_string(busNum) + "-" + std::to_string(devAddr))) {
854 USB_HILOGW(MODULE_USB_SERVICE, "remove right failed busNum:%{public}u devAddr:%{public}u", busNum, devAddr);
855 }
856
857 return usbHostManger_->DelDevice(busNum, devAddr);
858 }
859
UpdateUsbPort(int32_t portId,int32_t powerRole,int32_t dataRole,int32_t mode)860 void UsbService::UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode)
861 {
862 if (usbPortManager_ == nullptr) {
863 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbPortManager_");
864 return;
865 }
866
867 usbPortManager_->UpdatePort(portId, powerRole, dataRole, mode);
868 }
869
UpdateDeviceState(int32_t status)870 void UsbService::UpdateDeviceState(int32_t status)
871 {
872 if (usbDeviceManager_ == nullptr) {
873 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbDeviceManager_");
874 return;
875 }
876
877 usbDeviceManager_->HandleEvent(status);
878 }
879
GetBundleName(std::string & bundleName)880 bool UsbService::GetBundleName(std::string &bundleName)
881 {
882 #ifdef USB_RIGHT_TEST
883 bundleName = "com.usb.right";
884 return true;
885 #endif
886 pid_t uid = GetCallingUid();
887 sptr<ISystemAbilityManager> systemAbilityManager =
888 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
889 if (systemAbilityManager == nullptr) {
890 return false;
891 }
892 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
893 if (remoteObject == nullptr) {
894 return false;
895 }
896
897 sptr<AppExecFwk::IBundleMgr> bundleMgr(new AppExecFwk::BundleMgrProxy(remoteObject));
898 if (bundleMgr == nullptr) {
899 return false;
900 }
901 bundleMgr->GetBundleNameForUid(uid, bundleName);
902 return true;
903 }
904
RegBulkCallback(const UsbDev & devInfo,const UsbPipe & pipe,const sptr<IRemoteObject> & cb)905 int32_t UsbService::RegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe, const sptr<IRemoteObject> &cb)
906 {
907 if (cb == nullptr) {
908 USB_HILOGE(MODULE_USB_SERVICE, "cb is nullptr");
909 return UEC_SERVICE_INVALID_VALUE;
910 }
911 if (hdiCb_ == nullptr) {
912 hdiCb_ = new UsbdBulkCallbackImpl(cb);
913 }
914 if (usbd_ == nullptr) {
915 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
916 return UEC_SERVICE_INVALID_VALUE;
917 }
918
919 int32_t ret = usbd_->RegBulkCallback(devInfo, pipe, hdiCb_);
920 if (ret != UEC_OK) {
921 USB_HILOGE(MODULE_USB_SERVICE, "RegBulkCallback error ret:%{public}d", ret);
922 }
923 return ret;
924 }
925
UnRegBulkCallback(const UsbDev & devInfo,const UsbPipe & pipe)926 int32_t UsbService::UnRegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe)
927 {
928 if (usbd_ == nullptr) {
929 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
930 return UEC_SERVICE_INVALID_VALUE;
931 }
932
933 hdiCb_ = nullptr;
934 int32_t ret = usbd_->UnRegBulkCallback(devInfo, pipe);
935 if (ret != UEC_OK) {
936 USB_HILOGE(MODULE_USB_SERVICE, "UnRegBulkCallback error ret:%{public}d", ret);
937 }
938 return ret;
939 }
940
BulkRead(const UsbDev & devInfo,const UsbPipe & pipe,sptr<Ashmem> & ashmem)941 int32_t UsbService::BulkRead(const UsbDev &devInfo, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
942 {
943 if (ashmem == nullptr) {
944 USB_HILOGE(MODULE_USB_SERVICE, "BulkRead error ashmem");
945 return UEC_SERVICE_INVALID_VALUE;
946 }
947
948 if (usbd_ == nullptr) {
949 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
950 return UEC_SERVICE_INVALID_VALUE;
951 }
952 int32_t ret = usbd_->BulkRead(devInfo, pipe, ashmem);
953 if (ret != UEC_OK) {
954 USB_HILOGE(MODULE_USB_SERVICE, "BulkRead error ret:%{public}d", ret);
955 }
956 return ret;
957 }
958
BulkWrite(const UsbDev & devInfo,const UsbPipe & pipe,sptr<Ashmem> & ashmem)959 int32_t UsbService::BulkWrite(const UsbDev &devInfo, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
960 {
961 if (ashmem == nullptr) {
962 USB_HILOGE(MODULE_USB_SERVICE, "BulkWrite error ashmem");
963 return UEC_SERVICE_INVALID_VALUE;
964 }
965
966 if (usbd_ == nullptr) {
967 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
968 return UEC_SERVICE_INVALID_VALUE;
969 }
970 int32_t ret = usbd_->BulkWrite(devInfo, pipe, ashmem);
971 if (ret != UEC_OK) {
972 USB_HILOGE(MODULE_USB_SERVICE, "BulkWrite error ret:%{public}d", ret);
973 }
974 return ret;
975 }
976
BulkCancel(const UsbDev & devInfo,const UsbPipe & pipe)977 int32_t UsbService::BulkCancel(const UsbDev &devInfo, const UsbPipe &pipe)
978 {
979 if (usbd_ == nullptr) {
980 USB_HILOGE(MODULE_USB_SERVICE, "UsbService::usbd_ is nullptr");
981 return UEC_SERVICE_INVALID_VALUE;
982 }
983 int32_t ret = usbd_->BulkCancel(devInfo, pipe);
984 if (ret != UEC_OK) {
985 USB_HILOGE(MODULE_USB_SERVICE, "BulkCancel error ret:%{public}d", ret);
986 }
987 return ret;
988 }
989
AddRight(const std::string & bundleName,const std::string & deviceName)990 int32_t UsbService::AddRight(const std::string &bundleName, const std::string &deviceName)
991 {
992 USB_HILOGI(MODULE_USB_SERVICE, "calling AddRight");
993 if (usbRightManager_ == nullptr) {
994 USB_HILOGE(MODULE_USB_SERVICE, "invalid usbRightManager_");
995 return UEC_SERVICE_INVALID_VALUE;
996 }
997 if (!(usbRightManager_->IsSystemHap())) {
998 USB_HILOGW(MODULE_USB_SERVICE, "is not system app");
999 return UEC_SERVICE_PERMISSION_DENIED_SYSAPI;
1000 }
1001 USB_HILOGI(MODULE_USB_SERVICE, "AddRight bundleName = %{public}s, deviceName = %{public}s", bundleName.c_str(),
1002 deviceName.c_str());
1003 return usbRightManager_->AddDeviceRight(deviceName, bundleName);
1004 }
1005
Dump(int fd,const std::vector<std::u16string> & args)1006 int UsbService::Dump(int fd, const std::vector<std::u16string> &args)
1007 {
1008 if (fd < 0) {
1009 USB_HILOGE(MODULE_USB_SERVICE, "fd is invalid fd:%{public}d", fd);
1010 return UEC_SERVICE_INVALID_VALUE;
1011 }
1012
1013 std::vector<std::string> argList;
1014 std::transform(args.begin(), args.end(), std::back_inserter(argList), [](const std::u16string &arg) {
1015 return Str16ToStr8(arg);
1016 });
1017
1018 if (argList.empty()) {
1019 USB_HILOGE(MODULE_USB_SERVICE, "argList is empty");
1020 DumpHelp(fd);
1021 return UEC_SERVICE_INVALID_VALUE;
1022 }
1023
1024 if (argList[0] == USB_HOST) {
1025 usbHostManger_->Dump(fd, argList[1]);
1026 } else if (argList[0] == USB_DEVICE) {
1027 usbDeviceManager_->Dump(fd, argList[1]);
1028 } else if (argList[0] == USB_PORT) {
1029 usbPortManager_->Dump(fd, argList[1]);
1030 } else if (argList[0] == USB_HELP) {
1031 DumpHelp(fd);
1032 } else {
1033 dprintf(fd, "Usb Dump service:invalid parameter.\n");
1034 DumpHelp(fd);
1035 }
1036 return UEC_OK;
1037 }
1038
DumpHelp(int32_t fd)1039 void UsbService::DumpHelp(int32_t fd)
1040 {
1041 dprintf(fd, "ShowUsage:\n");
1042 dprintf(fd, " -h, --help: dump help\n");
1043 dprintf(fd, " usb_host -a: dump the all device list info\n");
1044 dprintf(fd, " usb_devive -a: dump the all device and function list info\n");
1045 dprintf(fd, " usb_port -a: dump the all device port status\n");
1046 }
1047 } // namespace USB
1048 } // namespace OHOS
1049