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_srv_client.h"
17 #include "datetime_ex.h"
18 #include "if_system_ability_manager.h"
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "string_ex.h"
22 #include "system_ability_definition.h"
23 #include "usb_common.h"
24 #include "usb_device.h"
25 #include "usb_errors.h"
26 #include "timer.h"
27 #include "v1_1/iusb_interface.h"
28
29 using namespace OHOS::HDI::Usb::V1_1;
30 namespace OHOS {
31 namespace USB {
32 constexpr uint32_t WAIT_SERVICE_LOAD = 500;
33 constexpr int32_t READ_BUF_SIZE = 8192;
UsbSrvClient()34 UsbSrvClient::UsbSrvClient()
35 {
36 Connect();
37 }
~UsbSrvClient()38 UsbSrvClient::~UsbSrvClient() {}
39
GetInstance()40 UsbSrvClient& UsbSrvClient::GetInstance()
41 {
42 static UsbSrvClient instance;
43 return instance;
44 }
45
Connect()46 int32_t UsbSrvClient::Connect()
47 {
48 std::lock_guard<std::mutex> lock(mutex_);
49 if (proxy_ != nullptr) {
50 return UEC_OK;
51 }
52 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
53 if (sm == nullptr) {
54 USB_HILOGE(MODULE_USB_INNERKIT, "fail to get SystemAbilityManager");
55 return UEC_INTERFACE_GET_SYSTEM_ABILITY_MANAGER_FAILED;
56 }
57 sptr<IRemoteObject> remoteObject = sm->CheckSystemAbility(USB_SYSTEM_ABILITY_ID);
58 if (remoteObject == nullptr) {
59 USB_HILOGE(MODULE_USB_INNERKIT, "GetSystemAbility failed.");
60 return UEC_INTERFACE_GET_USB_SERVICE_FAILED;
61 }
62 proxy_ = iface_cast<IUsbSrv>(remoteObject);
63 USB_HILOGI(MODULE_USB_INNERKIT, "Connect UsbService ok.");
64 sptr<IRemoteObject> deathObject = proxy_->AsObject();
65 if (deathObject == nullptr) {
66 USB_HILOGI(MODULE_USB_INNERKIT, "deathObject is null.");
67 return UEC_INTERFACE_DEAD_OBJECT;
68 }
69 deathRecipient_ = new UsbSrvDeathRecipient();
70 deathObject->AddDeathRecipient(deathRecipient_);
71 return UEC_OK;
72 }
73
ResetProxy(const wptr<IRemoteObject> & remote)74 void UsbSrvClient::ResetProxy(const wptr<IRemoteObject> &remote)
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 RETURN_IF(proxy_ == nullptr);
78 auto serviceRemote = proxy_->AsObject();
79 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
80 serviceRemote->RemoveDeathRecipient(deathRecipient_);
81
82 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_SERVICE_LOAD));
83
84 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
85 if (sm == nullptr) {
86 USB_HILOGE(MODULE_USB_INNERKIT, "fail to get SystemAbilityManager");
87 return;
88 }
89 sptr<IRemoteObject> remoteObject = sm->CheckSystemAbility(USB_SYSTEM_ABILITY_ID);
90 if (remoteObject == nullptr) {
91 USB_HILOGE(MODULE_USB_INNERKIT, "GetSystemAbility failed.");
92 proxy_ = nullptr;
93 return;
94 }
95 proxy_ = iface_cast<IUsbSrv>(remoteObject);
96 }
97 }
98
OnRemoteDied(const wptr<IRemoteObject> & remote)99 void UsbSrvClient::UsbSrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
100 {
101 if (remote == nullptr) {
102 USB_HILOGE(MODULE_USB_INNERKIT, "UsbSrvDeathRecipient::OnRemoteDied failed, remote is nullptr.");
103 return;
104 }
105 UsbSrvClient::GetInstance().ResetProxy(remote);
106 USB_HILOGI(MODULE_USB_INNERKIT, "UsbSrvDeathRecipient::Recv death notice.");
107 }
108
OpenDevice(const UsbDevice & device,USBDevicePipe & pipe)109 int32_t UsbSrvClient::OpenDevice(const UsbDevice &device, USBDevicePipe &pipe)
110 {
111 USB_HILOGI(MODULE_USB_INNERKIT, "Calling OpenDevice Start!");
112 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
113 int32_t ret = proxy_->OpenDevice(device.GetBusNum(), device.GetDevAddr());
114 if (ret != UEC_OK) {
115 USB_HILOGE(MODULE_USB_INNERKIT, "OpenDevice failed with ret = %{public}d !", ret);
116 return ret;
117 }
118
119 pipe.SetBusNum(device.GetBusNum());
120 pipe.SetDevAddr(device.GetDevAddr());
121 return UEC_OK;
122 }
123
HasRight(std::string deviceName)124 bool UsbSrvClient::HasRight(std::string deviceName)
125 {
126 USB_HILOGI(MODULE_USB_INNERKIT, "Calling HasRight Start!");
127 RETURN_IF_WITH_RET(Connect() != UEC_OK, false);
128 return proxy_->HasRight(deviceName);
129 }
130
RequestRight(std::string deviceName)131 int32_t UsbSrvClient::RequestRight(std::string deviceName)
132 {
133 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
134 int32_t ret = proxy_->RequestRight(deviceName);
135 if (ret != UEC_OK) {
136 USB_HILOGE(MODULE_USB_INNERKIT, "Calling RequestRight failed with ret = %{public}d !", ret);
137 }
138 return ret;
139 }
140
RemoveRight(std::string deviceName)141 int32_t UsbSrvClient::RemoveRight(std::string deviceName)
142 {
143 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
144 int32_t ret = proxy_->RemoveRight(deviceName);
145 if (ret != UEC_OK) {
146 USB_HILOGE(MODULE_USB_INNERKIT, "Calling RemoveRight failed with ret = %{public}d !", ret);
147 }
148 return ret;
149 }
150
GetDevices(std::vector<UsbDevice> & deviceList)151 int32_t UsbSrvClient::GetDevices(std::vector<UsbDevice> &deviceList)
152 {
153 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
154 int32_t ret = proxy_->GetDevices(deviceList);
155 if (ret != UEC_OK) {
156 USB_HILOGE(MODULE_USB_INNERKIT, "GetDevices failed ret = %{public}d!", ret);
157 return ret;
158 }
159 USB_HILOGI(MODULE_USB_INNERKIT, "GetDevices deviceList size = %{public}zu!", deviceList.size());
160 return ret;
161 }
162
GetCurrentFunctions(int32_t & funcs)163 int32_t UsbSrvClient::GetCurrentFunctions(int32_t &funcs)
164 {
165 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
166 int32_t ret = proxy_->GetCurrentFunctions(funcs);
167 if (ret != UEC_OK) {
168 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
169 return ret;
170 }
171 USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetCurrentFunctions Success!");
172 USB_HILOGI(MODULE_USB_INNERKIT, "GetCurrentFunctions funcs = %{public}d!", funcs);
173 return ret;
174 }
175
SetCurrentFunctions(int32_t funcs)176 int32_t UsbSrvClient::SetCurrentFunctions(int32_t funcs)
177 {
178 USB_HILOGI(MODULE_USB_INNERKIT, "SetCurrentFunctions funcs = %{public}d!", funcs);
179 RETURN_IF_WITH_RET(Connect() != UEC_OK, false);
180 int32_t ret = proxy_->SetCurrentFunctions(funcs);
181 if (ret != UEC_OK) {
182 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
183 return ret;
184 }
185 USB_HILOGI(MODULE_USB_INNERKIT, " Calling SetCurrentFunctions Success!");
186 return ret;
187 }
188
UsbFunctionsFromString(std::string_view funcs)189 int32_t UsbSrvClient::UsbFunctionsFromString(std::string_view funcs)
190 {
191 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
192 int32_t result = proxy_->UsbFunctionsFromString(funcs);
193 USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsFromString Success!");
194 return result;
195 }
196
UsbFunctionsToString(int32_t funcs)197 std::string UsbSrvClient::UsbFunctionsToString(int32_t funcs)
198 {
199 std::string result;
200 RETURN_IF_WITH_RET(Connect() != UEC_OK, result);
201 result = proxy_->UsbFunctionsToString(funcs);
202 USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsToString Success!");
203 return result;
204 }
205
GetPorts(std::vector<UsbPort> & usbports)206 int32_t UsbSrvClient::GetPorts(std::vector<UsbPort> &usbports)
207 {
208 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
209 USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetPorts");
210 int32_t ret = proxy_->GetPorts(usbports);
211 if (ret != UEC_OK) {
212 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
213 }
214 return ret;
215 }
216
GetSupportedModes(int32_t portId,int32_t & result)217 int32_t UsbSrvClient::GetSupportedModes(int32_t portId, int32_t &result)
218 {
219 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
220 USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetSupportedModes");
221 int32_t ret = proxy_->GetSupportedModes(portId, result);
222 if (ret != UEC_OK) {
223 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
224 }
225 return ret;
226 }
227
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)228 int32_t UsbSrvClient::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
229 {
230 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
231 USB_HILOGI(MODULE_USB_INNERKIT, "Calling SetPortRole");
232 int32_t ret = proxy_->SetPortRole(portId, powerRole, dataRole);
233 if (ret != UEC_OK) {
234 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
235 }
236 return ret;
237 }
238
ClaimInterface(USBDevicePipe & pipe,const UsbInterface & interface,bool force)239 int32_t UsbSrvClient::ClaimInterface(USBDevicePipe &pipe, const UsbInterface &interface, bool force)
240 {
241 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
242 int32_t ret = proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), force);
243 if (ret != UEC_OK) {
244 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
245 }
246 return ret;
247 }
248
ReleaseInterface(USBDevicePipe & pipe,const UsbInterface & interface)249 int32_t UsbSrvClient::ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &interface)
250 {
251 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
252 int32_t ret = proxy_->ReleaseInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId());
253 if (ret != UEC_OK) {
254 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
255 }
256 return ret;
257 }
258
BulkTransfer(USBDevicePipe & pipe,const USBEndpoint & endpoint,std::vector<uint8_t> & bufferData,int32_t timeOut)259 int32_t UsbSrvClient::BulkTransfer(
260 USBDevicePipe &pipe, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData, int32_t timeOut)
261 {
262 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
263 int32_t ret = UEC_INTERFACE_INVALID_VALUE;
264 const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
265 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
266 if (USB_ENDPOINT_DIR_IN == endpoint.GetDirection()) {
267 int32_t length = bufferData.size() > 0 ? static_cast<int32_t>(bufferData.size()) : READ_BUF_SIZE;
268 ret = proxy_->BulkTransferReadwithLength(tdev, tpipe, length, bufferData, timeOut);
269 } else if (USB_ENDPOINT_DIR_OUT == endpoint.GetDirection()) {
270 ret = proxy_->BulkTransferWrite(tdev, tpipe, bufferData, timeOut);
271 }
272 if (ret != UEC_OK) {
273 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
274 }
275 return ret;
276 }
277
ControlTransfer(USBDevicePipe & pipe,const UsbCtrlTransfer & ctrl,std::vector<uint8_t> & bufferData)278 int32_t UsbSrvClient::ControlTransfer(
279 USBDevicePipe &pipe, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
280 {
281 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
282 const UsbDev dev = {pipe.GetBusNum(), pipe.GetDevAddr()};
283 int32_t ret = proxy_->ControlTransfer(dev, ctrl, bufferData);
284 if (ret != UEC_OK) {
285 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
286 }
287
288 return ret;
289 }
290
UsbControlTransfer(USBDevicePipe & pipe,const HDI::Usb::V1_1::UsbCtrlTransferParams & ctrlParams,std::vector<uint8_t> & bufferData)291 int32_t UsbSrvClient::UsbControlTransfer(USBDevicePipe &pipe, const HDI::Usb::V1_1::UsbCtrlTransferParams &ctrlParams,
292 std::vector<uint8_t> &bufferData)
293 {
294 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
295 const UsbDev dev = {pipe.GetBusNum(), pipe.GetDevAddr()};
296 int32_t ret = proxy_->UsbControlTransfer(dev, ctrlParams, bufferData);
297 if (ret != UEC_OK) {
298 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
299 }
300
301 return ret;
302 }
303
SetConfiguration(USBDevicePipe & pipe,const USBConfig & config)304 int32_t UsbSrvClient::SetConfiguration(USBDevicePipe &pipe, const USBConfig &config)
305 {
306 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
307 int32_t ret = proxy_->SetActiveConfig(pipe.GetBusNum(), pipe.GetDevAddr(), config.GetId());
308 return ret;
309 }
310
SetInterface(USBDevicePipe & pipe,const UsbInterface & interface)311 int32_t UsbSrvClient::SetInterface(USBDevicePipe &pipe, const UsbInterface &interface)
312 {
313 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
314 return proxy_->SetInterface(
315 pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), interface.GetAlternateSetting());
316 }
317
GetRawDescriptors(USBDevicePipe & pipe,std::vector<uint8_t> & bufferData)318 int32_t UsbSrvClient::GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData)
319 {
320 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
321 int32_t ret = proxy_->GetRawDescriptor(pipe.GetBusNum(), pipe.GetDevAddr(), bufferData);
322 if (ret != UEC_OK) {
323 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
324 }
325 return ret;
326 }
327
GetFileDescriptor(USBDevicePipe & pipe,int32_t & fd)328 int32_t UsbSrvClient::GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd)
329 {
330 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
331 int32_t ret = proxy_->GetFileDescriptor(pipe.GetBusNum(), pipe.GetDevAddr(), fd);
332 if (ret != UEC_OK) {
333 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
334 }
335 return ret;
336 }
337
Close(const USBDevicePipe & pipe)338 bool UsbSrvClient::Close(const USBDevicePipe &pipe)
339 {
340 RETURN_IF_WITH_RET(proxy_ == nullptr, false);
341 int32_t ret = proxy_->Close(pipe.GetBusNum(), pipe.GetDevAddr());
342 return (ret == UEC_OK);
343 }
344
PipeRequestWait(USBDevicePipe & pipe,int64_t timeOut,UsbRequest & req)345 int32_t UsbSrvClient::PipeRequestWait(USBDevicePipe &pipe, int64_t timeOut, UsbRequest &req)
346 {
347 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
348 std::vector<uint8_t> clientData;
349 std::vector<uint8_t> bufferData;
350 const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
351 int32_t ret = proxy_->RequestWait(tdev, timeOut, clientData, bufferData);
352 if (ret != UEC_OK) {
353 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d.", ret);
354 return ret;
355 }
356
357 req.SetPipe(pipe);
358 req.SetClientData(clientData);
359 req.SetReqData(bufferData);
360 return ret;
361 }
362
RequestInitialize(UsbRequest & request)363 int32_t UsbSrvClient::RequestInitialize(UsbRequest &request)
364 {
365 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
366 const USBDevicePipe &pipe = request.GetPipe();
367 const USBEndpoint &endpoint = request.GetEndpoint();
368 return proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), endpoint.GetInterfaceId(), CLAIM_FORCE_1);
369 }
370
RequestFree(UsbRequest & request)371 int32_t UsbSrvClient::RequestFree(UsbRequest &request)
372 {
373 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
374 const USBDevicePipe &pipe = request.GetPipe();
375 const USBEndpoint &ep = request.GetEndpoint();
376 return proxy_->RequestCancel(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress());
377 }
378
RequestAbort(UsbRequest & request)379 int32_t UsbSrvClient::RequestAbort(UsbRequest &request)
380 {
381 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
382 const USBDevicePipe &pipe = request.GetPipe();
383 const USBEndpoint &ep = request.GetEndpoint();
384 return proxy_->RequestCancel(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress());
385 }
386
RequestQueue(UsbRequest & request)387 int32_t UsbSrvClient::RequestQueue(UsbRequest &request)
388 {
389 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
390 const USBDevicePipe &pipe = request.GetPipe();
391 const USBEndpoint &ep = request.GetEndpoint();
392 const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
393 const UsbPipe tpipe = {ep.GetInterfaceId(), ep.GetAddress()};
394 return proxy_->RequestQueue(tdev, tpipe, request.GetClientData(), request.GetReqData());
395 }
396
RegBulkCallback(USBDevicePipe & pip,const USBEndpoint & endpoint,const sptr<IRemoteObject> & cb)397 int32_t UsbSrvClient::RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb)
398 {
399 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
400 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
401 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
402 int32_t ret = proxy_->RegBulkCallback(tdev, tpipe, cb);
403 if (ret != UEC_OK) {
404 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
405 }
406 return ret;
407 }
408
UnRegBulkCallback(USBDevicePipe & pip,const USBEndpoint & endpoint)409 int32_t UsbSrvClient::UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint)
410 {
411 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
412 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
413 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
414 int32_t ret = proxy_->UnRegBulkCallback(tdev, tpipe);
415 if (ret != UEC_OK) {
416 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
417 }
418 return ret;
419 }
420
BulkRead(USBDevicePipe & pip,const USBEndpoint & endpoint,sptr<Ashmem> & ashmem)421 int32_t UsbSrvClient::BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
422 {
423 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
424 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
425 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
426 int32_t ret = proxy_->BulkRead(tdev, tpipe, ashmem);
427 if (ret != UEC_OK) {
428 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
429 }
430 return ret;
431 }
432
BulkWrite(USBDevicePipe & pip,const USBEndpoint & endpoint,sptr<Ashmem> & ashmem)433 int32_t UsbSrvClient::BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
434 {
435 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
436 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
437 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
438 int32_t ret = proxy_->BulkWrite(tdev, tpipe, ashmem);
439 if (ret != UEC_OK) {
440 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
441 }
442 return ret;
443 }
444
BulkCancel(USBDevicePipe & pip,const USBEndpoint & endpoint)445 int32_t UsbSrvClient::BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint)
446 {
447 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
448 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
449 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
450 int32_t ret = proxy_->BulkCancel(tdev, tpipe);
451 if (ret != UEC_OK) {
452 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
453 }
454 return ret;
455 }
456
AddRight(const std::string & bundleName,const std::string & deviceName)457 int32_t UsbSrvClient::AddRight(const std::string &bundleName, const std::string &deviceName)
458 {
459 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
460 USB_HILOGI(MODULE_USB_INNERKIT, "Calling AddRight");
461 int32_t ret = proxy_->AddRight(bundleName, deviceName);
462 if (ret != UEC_OK) {
463 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
464 }
465 return ret;
466 }
467
AddAccessRight(const std::string & tokenId,const std::string & deviceName)468 int32_t UsbSrvClient::AddAccessRight(const std::string &tokenId, const std::string &deviceName)
469 {
470 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
471 USB_HILOGI(MODULE_USB_INNERKIT, "Calling AddAccessRight");
472 int32_t ret = proxy_->AddAccessRight(tokenId, deviceName);
473 if (ret != UEC_OK) {
474 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
475 }
476 return ret;
477 }
478
ManageGlobalInterface(bool disable)479 int32_t UsbSrvClient::ManageGlobalInterface(bool disable)
480 {
481 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
482 int32_t ret = proxy_->ManageGlobalInterface(disable);
483 if (ret != UEC_OK) {
484 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
485 }
486 return ret;
487 }
488
ManageDevice(int32_t vendorId,int32_t productId,bool disable)489 int32_t UsbSrvClient::ManageDevice(int32_t vendorId, int32_t productId, bool disable)
490 {
491 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
492 int32_t ret = proxy_->ManageDevice(vendorId, productId, disable);
493 if (ret != UEC_OK) {
494 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
495 }
496 return ret;
497 }
498
ManageInterfaceType(const std::vector<UsbDeviceType> & disableType,bool disable)499 int32_t UsbSrvClient::ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable)
500 {
501 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
502 int32_t ret = proxy_->ManageInterfaceType(disableType, disable);
503 if (ret != UEC_OK) {
504 USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
505 }
506 return ret;
507 }
508
GetDeviceSpeed(USBDevicePipe & pipe,uint8_t & speed)509 int32_t UsbSrvClient::GetDeviceSpeed(USBDevicePipe &pipe, uint8_t &speed)
510 {
511 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
512 int32_t ret = proxy_->GetDeviceSpeed(pipe.GetBusNum(), pipe.GetDevAddr(), speed);
513 if (ret != UEC_OK) {
514 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
515 }
516 USB_HILOGE(MODULE_USB_INNERKIT, "GetDeviceSpeed speed = %{public}u!", speed);
517 return ret;
518 }
519
GetInterfaceActiveStatus(USBDevicePipe & pipe,const UsbInterface & interface,bool & unactivated)520 int32_t UsbSrvClient::GetInterfaceActiveStatus(USBDevicePipe &pipe, const UsbInterface &interface, bool &unactivated)
521 {
522 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
523 int32_t ret = proxy_->GetInterfaceActiveStatus(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), unactivated);
524 if (ret != UEC_OK) {
525 USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
526 }
527 return ret;
528 }
529
GetAccessoryList(std::vector<USBAccessory> & accessList)530 int32_t UsbSrvClient::GetAccessoryList(std::vector<USBAccessory> &accessList)
531 {
532 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
533 int32_t ret = proxy_->GetAccessoryList(accessList);
534 if (ret != UEC_OK) {
535 USB_HILOGE(MODULE_USB_INNERKIT, "GetAccessoryList failed ret = %{public}d!", ret);
536 return ret;
537 }
538 USB_HILOGI(MODULE_USB_INNERKIT, "GetAccessoryList accessList size = %{public}zu!", accessList.size());
539 return ret;
540 }
541
OpenAccessory(const USBAccessory & access,int32_t & fd)542 int32_t UsbSrvClient::OpenAccessory(const USBAccessory &access, int32_t &fd)
543 {
544 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
545 int32_t ret = proxy_->OpenAccessory(access, fd);
546 if (ret != UEC_OK) {
547 USB_HILOGE(MODULE_USB_INNERKIT, "OpenAccessory ret = %{public}d!", ret);
548 }
549 return ret;
550 }
551
AddAccessoryRight(const uint32_t tokenId,const USBAccessory & access)552 int32_t UsbSrvClient::AddAccessoryRight(const uint32_t tokenId, const USBAccessory &access)
553 {
554 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
555 int32_t ret = proxy_->AddAccessoryRight(tokenId, access);
556 if (ret != UEC_OK) {
557 USB_HILOGE(MODULE_USB_INNERKIT, "AddAccessoryRight ret = %{public}d!", ret);
558 }
559 return ret;
560 }
561
HasAccessoryRight(const USBAccessory & access,bool & result)562 int32_t UsbSrvClient::HasAccessoryRight(const USBAccessory &access, bool &result)
563 {
564 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
565 return proxy_->HasAccessoryRight(access, result);
566 }
567
RequestAccessoryRight(const USBAccessory & access,bool & result)568 int32_t UsbSrvClient::RequestAccessoryRight(const USBAccessory &access, bool &result)
569 {
570 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
571 int32_t ret = proxy_->RequestAccessoryRight(access, result);
572 if (ret != UEC_OK) {
573 USB_HILOGE(MODULE_USB_INNERKIT, "RequestAccessoryRight ret = %{public}d!", ret);
574 }
575 return ret;
576 }
577
CancelAccessoryRight(const USBAccessory & access)578 int32_t UsbSrvClient::CancelAccessoryRight(const USBAccessory &access)
579 {
580 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
581 int32_t ret = proxy_->CancelAccessoryRight(access);
582 if (ret != UEC_OK) {
583 USB_HILOGE(MODULE_USB_INNERKIT, "CancelAccessoryRight ret = %{public}d!", ret);
584 }
585 return ret;
586 }
587
CloseAccessory(const int32_t fd)588 int32_t UsbSrvClient::CloseAccessory(const int32_t fd)
589 {
590 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
591 int32_t ret = proxy_->CloseAccessory(fd);
592 if (ret != UEC_OK) {
593 USB_HILOGE(MODULE_USB_INNERKIT, "CloseAccessory ret = %{public}d!", ret);
594 }
595 return ret;
596 }
597
598 } // namespace USB
599 } // namespace OHOS
600