1 /*
2 * Copyright (c) 2021 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 <sstream>
18 #include "datetime_ex.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24 #include "usb_common.h"
25 #include "usb_device.h"
26 #include "usb_errors.h"
27
28 namespace OHOS {
29 namespace USB {
30 #define USB_MAX_REQUEST_DATA_SIZE 1024
31 const uint8_t CLAIM_FORCE_1 = 1;
UsbSrvClient()32 UsbSrvClient::UsbSrvClient()
33 {
34 Connect();
35 }
~UsbSrvClient()36 UsbSrvClient::~UsbSrvClient() {}
37
Connect()38 int32_t UsbSrvClient::Connect()
39 {
40 std::lock_guard<std::mutex> lock(mutex_);
41 if (proxy_ != nullptr) {
42 return UEC_OK;
43 }
44 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45 if (sm == nullptr) {
46 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:fail to get Registry", __func__);
47 return UEC_INTERFACE_GET_SYSTEM_ABILITY_MANAGER_FAILED;
48 }
49 sptr<IRemoteObject> remoteObject_ = sm->CheckSystemAbility(USB_SYSTEM_ABILITY_ID);
50 if (remoteObject_ == nullptr) {
51 USB_HILOGE(MODULE_USB_INNERKIT, "GetSystemAbility failed.");
52 return UEC_INTERFACE_GET_USB_SERVICE_FAILED;
53 }
54 proxy_ = iface_cast<IUsbSrv>(remoteObject_);
55 USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s :Connect UsbService ok.", __func__);
56 return UEC_OK;
57 }
58
ResetProxy(const wptr<IRemoteObject> & remote)59 void UsbSrvClient::ResetProxy(const wptr<IRemoteObject> &remote)
60 {
61 std::lock_guard<std::mutex> lock(mutex_);
62 RETURN_IF(proxy_ == nullptr);
63 auto serviceRemote = proxy_->AsObject();
64 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
65 serviceRemote->RemoveDeathRecipient(deathRecipient_);
66 proxy_ = nullptr;
67 }
68 }
69
OnRemoteDied(const wptr<IRemoteObject> & remote)70 void UsbSrvClient::UsbSrvDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
71 {
72 if (remote == nullptr) {
73 USB_HILOGE(MODULE_USB_INNERKIT, "UsbSrvDeathRecipient::OnRemoteDied failed, remote is nullptr.");
74 return;
75 }
76 UsbSrvClient::GetInstance().ResetProxy(remote);
77 USB_HILOGI(MODULE_USB_INNERKIT, "UsbSrvDeathRecipient::Recv death notice.");
78 }
79
OpenDevice(const UsbDevice & device,USBDevicePipe & pipe)80 int32_t UsbSrvClient::OpenDevice(const UsbDevice &device, USBDevicePipe &pipe)
81 {
82 USB_HILOGI(MODULE_USB_INNERKIT, " Calling OpenDevice Start!");
83 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
84 int32_t ret = proxy_->OpenDevice(device.GetBusNum(), device.GetDevAddr());
85 if (ret != UEC_OK) {
86 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
87 return ret;
88 }
89
90 pipe.SetBusNum(device.GetBusNum());
91 pipe.SetDevAddr(device.GetDevAddr());
92 return UEC_OK;
93 }
94
HasRight(std::string deviceName)95 int32_t UsbSrvClient::HasRight(std::string deviceName)
96 {
97 USB_HILOGI(MODULE_USB_INNERKIT, " Calling HasRight Start!");
98 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
99 int32_t ret = proxy_->HasRight(deviceName);
100 if (ret != UEC_OK) {
101 USB_HILOGE(MODULE_USB_INNERKIT, " Calling HasRight False!");
102 }
103 return ret;
104 }
105
RequestRight(std::string deviceName)106 int32_t UsbSrvClient::RequestRight(std::string deviceName)
107 {
108 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
109 int32_t ret = proxy_->RequestRight(deviceName);
110 if (ret != UEC_OK) {
111 USB_HILOGE(MODULE_USB_INNERKIT, " Calling RequestRight False!");
112 }
113 return ret;
114 }
115
RemoveRight(std::string deviceName)116 int32_t UsbSrvClient::RemoveRight(std::string deviceName)
117 {
118 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
119 int32_t ret = proxy_->RemoveRight(deviceName);
120 if (ret != UEC_OK) {
121 USB_HILOGE(MODULE_USB_INNERKIT, " Calling RequestRight False!");
122 }
123 return ret;
124 }
125
GetDevices(std::vector<UsbDevice> & deviceList)126 int32_t UsbSrvClient::GetDevices(std::vector<UsbDevice> &deviceList)
127 {
128 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
129 int32_t ret = proxy_->GetDevices(deviceList);
130 if (ret != UEC_OK) {
131 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
132 }
133 USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s list size = %{public}d!", __func__, deviceList.size());
134 return ret;
135 }
136
GetCurrentFunctions(int32_t & funcs)137 int32_t UsbSrvClient::GetCurrentFunctions(int32_t &funcs)
138 {
139 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
140 int32_t ret = proxy_->GetCurrentFunctions(funcs);
141 if (ret != UEC_OK) {
142 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
143 }
144 USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetCurrentFunctions Success!");
145 return ret;
146 }
SetCurrentFunctions(int32_t funcs)147 int32_t UsbSrvClient::SetCurrentFunctions(int32_t funcs)
148 {
149 RETURN_IF_WITH_RET(Connect() != UEC_OK, false);
150 int32_t ret = proxy_->SetCurrentFunctions(funcs);
151 if (ret != UEC_OK) {
152 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
153 return ret;
154 }
155 USB_HILOGI(MODULE_USB_INNERKIT, " Calling SetCurrentFunctions Success!");
156 return ret;
157 }
158
UsbFunctionsFromString(std::string funcs)159 int32_t UsbSrvClient::UsbFunctionsFromString(std::string funcs)
160 {
161 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
162 int32_t result = proxy_->UsbFunctionsFromString(funcs);
163 USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsFromString Success!");
164 return result;
165 }
166
UsbFunctionsToString(int32_t funcs)167 std::string UsbSrvClient::UsbFunctionsToString(int32_t funcs)
168 {
169 std::string result;
170 RETURN_IF_WITH_RET(Connect() != UEC_OK, result);
171 result = proxy_->UsbFunctionsToString(funcs);
172 USB_HILOGI(MODULE_USB_INNERKIT, " Calling UsbFunctionsToString Success!");
173 return result;
174 }
175
GetPorts(std::vector<UsbPort> & usbports)176 int32_t UsbSrvClient::GetPorts(std::vector<UsbPort> &usbports)
177 {
178 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
179 USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetPorts");
180 int32_t ret = proxy_->GetPorts(usbports);
181 if (ret != UEC_OK) {
182 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
183 }
184 return ret;
185 }
186
GetSupportedModes(int32_t portId,int32_t & result)187 int32_t UsbSrvClient::GetSupportedModes(int32_t portId, int32_t &result)
188 {
189 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
190 USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetSupportedModes");
191 int32_t ret = proxy_->GetSupportedModes(portId, result);
192 if (ret != UEC_OK) {
193 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
194 }
195 return ret;
196 }
197
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)198 int32_t UsbSrvClient::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
199 {
200 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
201 USB_HILOGI(MODULE_USB_INNERKIT, " Calling SetPortRole");
202 int32_t ret = proxy_->SetPortRole(portId, powerRole, dataRole);
203 if (ret != UEC_OK) {
204 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
205 }
206 return ret;
207 }
208
ClaimInterface(USBDevicePipe & pipe,const UsbInterface & interface,bool force)209 int32_t UsbSrvClient::ClaimInterface(USBDevicePipe &pipe, const UsbInterface &interface, bool force)
210 {
211 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
212 int32_t ret = proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(), force);
213 if (ret != UEC_OK) {
214 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
215 }
216 return ret;
217 }
ReleaseInterface(USBDevicePipe & pipe,const UsbInterface & interface)218 int32_t UsbSrvClient::ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &interface)
219 {
220 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
221 int32_t ret = proxy_->ReleaseInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId());
222 if (ret != UEC_OK) {
223 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
224 }
225 return ret;
226 }
BulkTransfer(USBDevicePipe & pipe,const USBEndpoint & endpoint,std::vector<uint8_t> & bufferData,int32_t timeOut)227 int32_t UsbSrvClient::BulkTransfer(USBDevicePipe &pipe, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData,
228 int32_t timeOut)
229 {
230 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
231 int32_t ret = UEC_INTERFACE_INVALID_VALUE;
232 const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
233 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
234 if (USB_ENDPOINT_DIR_IN == endpoint.GetDirection()) {
235 ret = proxy_->BulkTransferRead(tdev, tpipe, bufferData, timeOut);
236 } else if (USB_ENDPOINT_DIR_OUT == endpoint.GetDirection()) {
237 ret = proxy_->BulkTransferWrite(tdev, tpipe, bufferData, timeOut);
238 }
239 if (ret != UEC_OK) {
240 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
241 }
242 return ret;
243 }
ControlTransfer(USBDevicePipe & pipe,const UsbCtrlTransfer & ctrl,std::vector<uint8_t> & bufferData)244 int32_t UsbSrvClient::ControlTransfer(USBDevicePipe &pipe, const UsbCtrlTransfer &ctrl,
245 std::vector<uint8_t> &bufferData)
246 {
247 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
248 const UsbDev dev = {pipe.GetBusNum(), pipe.GetDevAddr()};
249 int32_t ret = proxy_->ControlTransfer(dev, ctrl, bufferData);
250 if (ret != UEC_OK) {
251 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
252 }
253
254 return ret;
255 }
SetConfiguration(USBDevicePipe & pipe,const USBConfig & config)256 int32_t UsbSrvClient::SetConfiguration(USBDevicePipe &pipe, const USBConfig &config)
257 {
258 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
259 int32_t ret = proxy_->SetActiveConfig(pipe.GetBusNum(), pipe.GetDevAddr(), config.GetId());
260 return ret;
261 }
SetInterface(USBDevicePipe & pipe,const UsbInterface & interface)262 int32_t UsbSrvClient::SetInterface(USBDevicePipe &pipe, const UsbInterface &interface)
263 {
264 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
265 return proxy_->SetInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(),
266 interface.GetAlternateSetting());
267 }
GetRawDescriptors(USBDevicePipe & pipe,std::vector<uint8_t> & bufferData)268 int32_t UsbSrvClient::GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData)
269 {
270 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
271 int32_t ret = proxy_->GetRawDescriptor(pipe.GetBusNum(), pipe.GetDevAddr(), bufferData);
272 if (ret != UEC_OK) {
273 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
274 }
275 return ret;
276 }
GetFileDescriptor(USBDevicePipe & pipe,int32_t & fd)277 int32_t UsbSrvClient::GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd)
278 {
279 RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
280 int32_t ret = proxy_->GetFileDescriptor(pipe.GetBusNum(), pipe.GetDevAddr(), fd);
281 if (ret != UEC_OK) {
282 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s failed ret = %{public}d!", __func__, ret);
283 }
284 return ret;
285 }
286
Close(const USBDevicePipe & pipe)287 bool UsbSrvClient::Close(const USBDevicePipe &pipe)
288 {
289 RETURN_IF_WITH_RET(proxy_ == nullptr, false);
290 int32_t ret = proxy_->Close(pipe.GetBusNum(), pipe.GetDevAddr());
291 return (ret == UEC_OK);
292 }
293
PipeRequestWait(USBDevicePipe & pipe,int64_t timeOut,UsbRequest & req)294 int32_t UsbSrvClient::PipeRequestWait(USBDevicePipe &pipe, int64_t timeOut, UsbRequest &req)
295 {
296 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
297 std::vector<uint8_t> clientData;
298 std::vector<uint8_t> bufferData;
299 const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
300 int32_t ret = proxy_->RequestWait(tdev, timeOut, clientData, bufferData);
301 if (ret != UEC_OK) {
302 USB_HILOGE(MODULE_USB_INNERKIT, "UsbSrvClient::%{public}s:%{public}d :failed width ret = %{public}d.", __func__,
303 __LINE__, ret);
304 return ret;
305 }
306
307 req.SetPipe(pipe);
308 req.SetClientData(clientData);
309 req.SetReqData(bufferData);
310 return ret;
311 }
312
RequestInitialize(UsbRequest & request)313 int32_t UsbSrvClient::RequestInitialize(UsbRequest &request)
314 {
315 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
316 const USBDevicePipe &pipe = request.GetPipe();
317 const USBEndpoint &endpoint = request.GetEndpoint();
318 return proxy_->ClaimInterface(pipe.GetBusNum(), pipe.GetDevAddr(), endpoint.GetInterfaceId(), CLAIM_FORCE_1);
319 }
320
RequestFree(UsbRequest & request)321 int32_t UsbSrvClient::RequestFree(UsbRequest &request)
322 {
323 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
324 const USBDevicePipe &pipe = request.GetPipe();
325 const USBEndpoint &ep = request.GetEndpoint();
326 return proxy_->RequestCancel(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress());
327 }
328
RequestAbort(UsbRequest & request)329 int32_t UsbSrvClient::RequestAbort(UsbRequest &request)
330 {
331 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
332 const USBDevicePipe &pipe = request.GetPipe();
333 const USBEndpoint &ep = request.GetEndpoint();
334 return proxy_->RequestCancel(pipe.GetBusNum(), pipe.GetDevAddr(), ep.GetInterfaceId(), ep.GetAddress());
335 }
336
RequestQueue(UsbRequest & request)337 int32_t UsbSrvClient::RequestQueue(UsbRequest &request)
338 {
339 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
340 const USBDevicePipe &pipe = request.GetPipe();
341 const USBEndpoint &ep = request.GetEndpoint();
342 const UsbDev tdev = {pipe.GetBusNum(), pipe.GetDevAddr()};
343 const UsbPipe tpipe = {ep.GetInterfaceId(), ep.GetAddress()};
344 return proxy_->RequestQueue(tdev, tpipe, request.GetClientData(), request.GetReqData());
345 }
RegBulkCallback(USBDevicePipe & pip,const USBEndpoint & endpoint,const sptr<IRemoteObject> & cb)346 int32_t UsbSrvClient::RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb)
347 {
348 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
349 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
350 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
351 int32_t ret = proxy_->RegBulkCallback(tdev, tpipe, cb);
352 if (ret != UEC_OK) {
353 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
354 }
355 return ret;
356 }
UnRegBulkCallback(USBDevicePipe & pip,const USBEndpoint & endpoint)357 int32_t UsbSrvClient::UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint)
358 {
359 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
360 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
361 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
362 int32_t ret = proxy_->UnRegBulkCallback(tdev, tpipe);
363 if (ret != UEC_OK) {
364 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
365 }
366 return ret;
367 }
BulkRead(USBDevicePipe & pip,const USBEndpoint & endpoint,sptr<Ashmem> & ashmem)368 int32_t UsbSrvClient::BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
369 {
370 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
371 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
372 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
373 int32_t ret = proxy_->BulkRead(tdev, tpipe, ashmem);
374 if (ret != UEC_OK) {
375 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
376 }
377 return ret;
378 }
BulkWrite(USBDevicePipe & pip,const USBEndpoint & endpoint,sptr<Ashmem> & ashmem)379 int32_t UsbSrvClient::BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
380 {
381 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
382 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
383 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
384 int32_t ret = proxy_->BulkWrite(tdev, tpipe, ashmem);
385 if (ret != UEC_OK) {
386 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
387 }
388 return ret;
389 }
BulkCancel(USBDevicePipe & pip,const USBEndpoint & endpoint)390 int32_t UsbSrvClient::BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint)
391 {
392 RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
393 const UsbDev tdev = {pip.GetBusNum(), pip.GetDevAddr()};
394 const UsbPipe tpipe = {endpoint.GetInterfaceId(), endpoint.GetAddress()};
395 int32_t ret = proxy_->BulkCancel(tdev, tpipe);
396 if (ret != UEC_OK) {
397 USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s : failed width ret = %{public}d !", __func__, ret);
398 }
399 return ret;
400 }
401 } // namespace USB
402 } // namespace OHOS
403