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 
ResetDevice(const UsbDevice &device, USBDevicePipe &pipe)124 int32_t UsbSrvClient::ResetDevice(const UsbDevice &device, USBDevicePipe &pipe)
125 {
126     USB_HILOGI(MODULE_USB_INNERKIT, "Calling ResetDevice Start!");
127     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
128     int32_t ret = proxy_->ResetDevice(device.GetBusNum(), device.GetDevAddr());
129     if (ret != UEC_OK) {
130         USB_HILOGE(MODULE_USB_INNERKIT, "ResetDevice failed with ret = %{public}d !", ret);
131         return ret;
132     }
133 
134     pipe.SetBusNum(device.GetBusNum());
135     pipe.SetDevAddr(device.GetDevAddr());
136     return UEC_OK;
137 }
138 
HasRight(std::string deviceName)139 bool UsbSrvClient::HasRight(std::string deviceName)
140 {
141     USB_HILOGI(MODULE_USB_INNERKIT, "Calling HasRight Start!");
142     RETURN_IF_WITH_RET(Connect() != UEC_OK, false);
143     return proxy_->HasRight(deviceName);
144 }
145 
RequestRight(std::string deviceName)146 int32_t UsbSrvClient::RequestRight(std::string deviceName)
147 {
148     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
149     int32_t ret = proxy_->RequestRight(deviceName);
150     if (ret != UEC_OK) {
151         USB_HILOGE(MODULE_USB_INNERKIT, "Calling RequestRight failed with ret = %{public}d !", ret);
152     }
153     return ret;
154 }
155 
RemoveRight(std::string deviceName)156 int32_t UsbSrvClient::RemoveRight(std::string deviceName)
157 {
158     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
159     int32_t ret = proxy_->RemoveRight(deviceName);
160     if (ret != UEC_OK) {
161         USB_HILOGE(MODULE_USB_INNERKIT, "Calling RemoveRight failed with ret = %{public}d !", ret);
162     }
163     return ret;
164 }
165 
GetDevices(std::vector<UsbDevice> &deviceList)166 int32_t UsbSrvClient::GetDevices(std::vector<UsbDevice> &deviceList)
167 {
168     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
169     int32_t ret = proxy_->GetDevices(deviceList);
170     if (ret != UEC_OK) {
171         USB_HILOGE(MODULE_USB_INNERKIT, "GetDevices failed ret = %{public}d!", ret);
172         return ret;
173     }
174     USB_HILOGI(MODULE_USB_INNERKIT, "GetDevices deviceList size = %{public}zu!", deviceList.size());
175     return ret;
176 }
177 
GetCurrentFunctions(int32_t &funcs)178 int32_t UsbSrvClient::GetCurrentFunctions(int32_t &funcs)
179 {
180     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
181     int32_t ret = proxy_->GetCurrentFunctions(funcs);
182     if (ret != UEC_OK) {
183         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
184         return ret;
185     }
186     USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetCurrentFunctions Success!");
187     USB_HILOGI(MODULE_USB_INNERKIT, "GetCurrentFunctions funcs = %{public}d!", funcs);
188     return ret;
189 }
190 
SetCurrentFunctions(int32_t funcs)191 int32_t UsbSrvClient::SetCurrentFunctions(int32_t funcs)
192 {
193     USB_HILOGI(MODULE_USB_INNERKIT, "SetCurrentFunctions funcs = %{public}d!", funcs);
194     RETURN_IF_WITH_RET(Connect() != UEC_OK, false);
195     int32_t ret = proxy_->SetCurrentFunctions(funcs);
196     if (ret != UEC_OK) {
197         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
198         return ret;
199     }
200     USB_HILOGI(MODULE_USB_INNERKIT, " Calling SetCurrentFunctions Success!");
201     return ret;
202 }
203 
UsbFunctionsFromString(std::string_view funcs)204 int32_t UsbSrvClient::UsbFunctionsFromString(std::string_view funcs)
205 {
206     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
207     int32_t result = proxy_->UsbFunctionsFromString(funcs);
208     USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsFromString Success!");
209     return result;
210 }
211 
UsbFunctionsToString(int32_t funcs)212 std::string UsbSrvClient::UsbFunctionsToString(int32_t funcs)
213 {
214     std::string result;
215     RETURN_IF_WITH_RET(Connect() != UEC_OK, result);
216     result = proxy_->UsbFunctionsToString(funcs);
217     USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsToString Success!");
218     return result;
219 }
220 
GetPorts(std::vector<UsbPort> &usbports)221 int32_t UsbSrvClient::GetPorts(std::vector<UsbPort> &usbports)
222 {
223     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
224     USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetPorts");
225     int32_t ret = proxy_->GetPorts(usbports);
226     if (ret != UEC_OK) {
227         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
228     }
229     return ret;
230 }
231 
GetSupportedModes(int32_t portId, int32_t &result)232 int32_t UsbSrvClient::GetSupportedModes(int32_t portId, int32_t &result)
233 {
234     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
235     USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetSupportedModes");
236     int32_t ret = proxy_->GetSupportedModes(portId, result);
237     if (ret != UEC_OK) {
238         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
239     }
240     return ret;
241 }
242 
SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)243 int32_t UsbSrvClient::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
244 {
245     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
246     USB_HILOGI(MODULE_USB_INNERKIT, "Calling SetPortRole");
247     int32_t ret = proxy_->SetPortRole(portId, powerRole, dataRole);
248     if (ret != UEC_OK) {
249         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
250     }
251     return ret;
252 }
253 
ClaimInterface(USBDevicePipe &pipe, const UsbInterface &interface, bool force)254 int32_t UsbSrvClient::ClaimInterface(USBDevicePipe &pipe, const UsbInterface &interface, bool force)
255 {
256     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
257     int32_t ret = proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), force);
258     if (ret != UEC_OK) {
259         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
260     }
261     return ret;
262 }
263 
UsbAttachKernelDriver(USBDevicePipe &pipe, const UsbInterface &interface)264 int32_t UsbSrvClient::UsbAttachKernelDriver(USBDevicePipe &pipe, const UsbInterface &interface)
265 {
266     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
267     int32_t ret = proxy_->UsbAttachKernelDriver(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId());
268     if (ret != UEC_OK) {
269         USB_HILOGE(MODULE_USB_INNERKIT, "UsbAttachKernelDriver failed width ret = %{public}d !", ret);
270     }
271     return ret;
272 }
273 
UsbDetachKernelDriver(USBDevicePipe &pipe, const UsbInterface &interface)274 int32_t UsbSrvClient::UsbDetachKernelDriver(USBDevicePipe &pipe, const UsbInterface &interface)
275 {
276     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
277     int32_t ret = proxy_->UsbDetachKernelDriver(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId());
278     if (ret != UEC_OK) {
279         USB_HILOGE(MODULE_USB_INNERKIT, "UsbDetachKernelDriver failed width ret = %{public}d !", ret);
280     }
281     return ret;
282 }
283 
ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &interface)284 int32_t UsbSrvClient::ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &interface)
285 {
286     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
287     int32_t ret = proxy_->ReleaseInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId());
288     if (ret != UEC_OK) {
289         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
290     }
291     return ret;
292 }
293 
BulkTransfer( USBDevicePipe &pipe, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData, int32_t timeOut)294 int32_t UsbSrvClient::BulkTransfer(
295     USBDevicePipe &pipe, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData, int32_t timeOut)
296 {
297     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
298     int32_t ret = UEC_INTERFACE_INVALID_VALUE;
299     const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
300     const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
301     if (USB_ENDPOINT_DIR_IN == endpoint.GetDirection()) {
302         int32_t length = bufferData.size() > 0 ? static_cast<int32_t>(bufferData.size()) : READ_BUF_SIZE;
303         ret = proxy_->BulkTransferReadwithLength(tdev, tpipe, length, bufferData, timeOut);
304     } else if (USB_ENDPOINT_DIR_OUT == endpoint.GetDirection()) {
305         ret = proxy_->BulkTransferWrite(tdev, tpipe, bufferData, timeOut);
306     }
307     if (ret != UEC_OK) {
308         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
309     }
310     return ret;
311 }
312 
ControlTransfer( USBDevicePipe &pipe, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)313 int32_t UsbSrvClient::ControlTransfer(
314     USBDevicePipe &pipe, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
315 {
316     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
317     const UsbDev dev = {pipe.GetBusNum(), pipe.GetDevAddr()};
318     int32_t ret = proxy_->ControlTransfer(dev, ctrl, bufferData);
319     if (ret != UEC_OK) {
320         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
321     }
322 
323     return ret;
324 }
325 
UsbControlTransfer(USBDevicePipe &pipe, const HDI::Usb::V1_1::UsbCtrlTransferParams &ctrlParams, std::vector<uint8_t> &bufferData)326 int32_t UsbSrvClient::UsbControlTransfer(USBDevicePipe &pipe, const HDI::Usb::V1_1::UsbCtrlTransferParams &ctrlParams,
327     std::vector<uint8_t> &bufferData)
328 {
329     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
330     const UsbDev dev = {pipe.GetBusNum(), pipe.GetDevAddr()};
331     int32_t ret = proxy_->UsbControlTransfer(dev, ctrlParams, bufferData);
332     if (ret != UEC_OK) {
333         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
334     }
335 
336     return ret;
337 }
338 
SetConfiguration(USBDevicePipe &pipe, const USBConfig &config)339 int32_t UsbSrvClient::SetConfiguration(USBDevicePipe &pipe, const USBConfig &config)
340 {
341     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
342     int32_t ret = proxy_->SetActiveConfig(pipe.GetBusNum(), pipe.GetDevAddr(), config.GetId());
343     return ret;
344 }
345 
SetInterface(USBDevicePipe &pipe, const UsbInterface &interface)346 int32_t UsbSrvClient::SetInterface(USBDevicePipe &pipe, const UsbInterface &interface)
347 {
348     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
349     return proxy_->SetInterface(
350         pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), interface.GetAlternateSetting());
351 }
352 
GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData)353 int32_t UsbSrvClient::GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData)
354 {
355     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
356     int32_t ret = proxy_->GetRawDescriptor(pipe.GetBusNum(), pipe.GetDevAddr(), bufferData);
357     if (ret != UEC_OK) {
358         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
359     }
360     return ret;
361 }
362 
GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd)363 int32_t UsbSrvClient::GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd)
364 {
365     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
366     int32_t ret = proxy_->GetFileDescriptor(pipe.GetBusNum(), pipe.GetDevAddr(), fd);
367     if (ret != UEC_OK) {
368         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
369     }
370     return ret;
371 }
372 
Close(const USBDevicePipe &pipe)373 bool UsbSrvClient::Close(const USBDevicePipe &pipe)
374 {
375     RETURN_IF_WITH_RET(proxy_ == nullptr, false);
376     int32_t ret = proxy_->Close(pipe.GetBusNum(), pipe.GetDevAddr());
377     return (ret == UEC_OK);
378 }
379 
PipeRequestWait(USBDevicePipe &pipe, int64_t timeOut, UsbRequest &req)380 int32_t UsbSrvClient::PipeRequestWait(USBDevicePipe &pipe, int64_t timeOut, UsbRequest &req)
381 {
382     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
383     std::vector<uint8_t> clientData;
384     std::vector<uint8_t> bufferData;
385     const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
386     int32_t ret = proxy_->RequestWait(tdev, timeOut, clientData, bufferData);
387     if (ret != UEC_OK) {
388         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d.", ret);
389         return ret;
390     }
391 
392     req.SetPipe(pipe);
393     req.SetClientData(clientData);
394     req.SetReqData(bufferData);
395     return ret;
396 }
397 
RequestInitialize(UsbRequest &request)398 int32_t UsbSrvClient::RequestInitialize(UsbRequest &request)
399 {
400     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
401     const USBDevicePipe &pipe = request.GetPipe();
402     const USBEndpoint &endpoint = request.GetEndpoint();
403     return proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), endpoint.GetInterfaceId(), CLAIM_FORCE_1);
404 }
405 
RequestFree(UsbRequest &request)406 int32_t UsbSrvClient::RequestFree(UsbRequest &request)
407 {
408     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
409     const USBDevicePipe &pipe = request.GetPipe();
410     const USBEndpoint &ep = request.GetEndpoint();
411     return proxy_->RequestCancel(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress());
412 }
413 
RequestAbort(UsbRequest &request)414 int32_t UsbSrvClient::RequestAbort(UsbRequest &request)
415 {
416     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
417     const USBDevicePipe &pipe = request.GetPipe();
418     const USBEndpoint &ep = request.GetEndpoint();
419     return proxy_->RequestCancel(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress());
420 }
421 
RequestQueue(UsbRequest &request)422 int32_t UsbSrvClient::RequestQueue(UsbRequest &request)
423 {
424     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
425     const USBDevicePipe &pipe = request.GetPipe();
426     const USBEndpoint &ep = request.GetEndpoint();
427     const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
428     const UsbPipe tpipe = {ep.GetInterfaceId(), ep.GetAddress()};
429     return proxy_->RequestQueue(tdev, tpipe, request.GetClientData(), request.GetReqData());
430 }
431 
RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb)432 int32_t UsbSrvClient::RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb)
433 {
434     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
435     const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
436     const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
437     int32_t ret = proxy_->RegBulkCallback(tdev, tpipe, cb);
438     if (ret != UEC_OK) {
439         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
440     }
441     return ret;
442 }
443 
UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint)444 int32_t UsbSrvClient::UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint)
445 {
446     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
447     const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
448     const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
449     int32_t ret = proxy_->UnRegBulkCallback(tdev, tpipe);
450     if (ret != UEC_OK) {
451         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
452     }
453     return ret;
454 }
455 
BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)456 int32_t UsbSrvClient::BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
457 {
458     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
459     const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
460     const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
461     int32_t ret = proxy_->BulkRead(tdev, tpipe, ashmem);
462     if (ret != UEC_OK) {
463         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
464     }
465     return ret;
466 }
467 
BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)468 int32_t UsbSrvClient::BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
469 {
470     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
471     const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
472     const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
473     int32_t ret = proxy_->BulkWrite(tdev, tpipe, ashmem);
474     if (ret != UEC_OK) {
475         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
476     }
477     return ret;
478 }
479 
BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint)480 int32_t UsbSrvClient::BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint)
481 {
482     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
483     const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
484     const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
485     int32_t ret = proxy_->BulkCancel(tdev, tpipe);
486     if (ret != UEC_OK) {
487         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
488     }
489     return ret;
490 }
491 
AddRight(const std::string &bundleName, const std::string &deviceName)492 int32_t UsbSrvClient::AddRight(const std::string &bundleName, const std::string &deviceName)
493 {
494     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
495     USB_HILOGI(MODULE_USB_INNERKIT, "Calling AddRight");
496     int32_t ret = proxy_->AddRight(bundleName, deviceName);
497     if (ret != UEC_OK) {
498         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
499     }
500     return ret;
501 }
502 
AddAccessRight(const std::string &tokenId, const std::string &deviceName)503 int32_t UsbSrvClient::AddAccessRight(const std::string &tokenId, const std::string &deviceName)
504 {
505     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
506     USB_HILOGI(MODULE_USB_INNERKIT, "Calling AddAccessRight");
507     int32_t ret = proxy_->AddAccessRight(tokenId, deviceName);
508     if (ret != UEC_OK) {
509         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
510     }
511     return ret;
512 }
513 
ManageGlobalInterface(bool disable)514 int32_t UsbSrvClient::ManageGlobalInterface(bool disable)
515 {
516     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
517     int32_t ret = proxy_->ManageGlobalInterface(disable);
518     if (ret != UEC_OK) {
519         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
520     }
521     return ret;
522 }
523 
ManageDevice(int32_t vendorId, int32_t productId, bool disable)524 int32_t UsbSrvClient::ManageDevice(int32_t vendorId, int32_t productId, bool disable)
525 {
526     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
527     int32_t ret = proxy_->ManageDevice(vendorId, productId, disable);
528     if (ret != UEC_OK) {
529         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
530     }
531     return ret;
532 }
533 
ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable)534 int32_t UsbSrvClient::ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable)
535 {
536     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
537     int32_t ret = proxy_->ManageInterfaceType(disableType, disable);
538     if (ret != UEC_OK) {
539         USB_HILOGE(MODULE_USB_INNERKIT, "failed width ret = %{public}d !", ret);
540     }
541     return ret;
542 }
543 
ClearHalt(USBDevicePipe &pipe, const USBEndpoint &ep)544 int32_t UsbSrvClient::ClearHalt(USBDevicePipe &pipe, const USBEndpoint &ep)
545 {
546     RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
547     int32_t ret = proxy_->ClearHalt(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress());
548     if (ret != UEC_OK) {
549         USB_HILOGE(MODULE_USB_INNERKIT, "ClearHalt failed ret = %{public}d !", ret);
550     }
551     return ret;
552 }
553 
GetDeviceSpeed(USBDevicePipe &pipe, uint8_t &speed)554 int32_t UsbSrvClient::GetDeviceSpeed(USBDevicePipe &pipe, uint8_t &speed)
555 {
556     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
557     int32_t ret = proxy_->GetDeviceSpeed(pipe.GetBusNum(), pipe.GetDevAddr(), speed);
558     if (ret != UEC_OK) {
559         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
560     }
561     USB_HILOGE(MODULE_USB_INNERKIT, "GetDeviceSpeed speed = %{public}u!", speed);
562     return ret;
563 }
564 
GetInterfaceActiveStatus(USBDevicePipe &pipe, const UsbInterface &interface, bool &unactivated)565 int32_t UsbSrvClient::GetInterfaceActiveStatus(USBDevicePipe &pipe, const UsbInterface &interface, bool &unactivated)
566 {
567     RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
568     int32_t ret = proxy_->GetInterfaceActiveStatus(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), unactivated);
569     if (ret != UEC_OK) {
570         USB_HILOGE(MODULE_USB_INNERKIT, "failed ret = %{public}d!", ret);
571     }
572     return ret;
573 }
574 
575 } // namespace USB
576 } // namespace OHOS
577