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 "ipc_types.h"
17 #include "message_parcel.h"
18 #include "securec.h"
19 #include "string_ex.h"
20 #include "usb_common.h"
21 #include "usb_errors.h"
22 #include "usb_request.h"
23 #include "usb_server_proxy.h"
24 #include "v1_1/iusb_interface.h"
25
26 using namespace OHOS::HDI::Usb::V1_1;
27 namespace OHOS {
28 namespace USB {
29
30 constexpr int32_t MAX_DEVICE_NUM = 127;
31 constexpr int32_t MAX_CONFIG_NUM = 100;
32 constexpr int32_t MAX_INTERFACE_NUM = 100;
33 constexpr int32_t MAX_ENDPOINT_NUM = 32;
34 constexpr int32_t MAX_PORT_NUM = 100;
35
SetDeviceMessage(MessageParcel & data,uint8_t busNum,uint8_t devAddr)36 int32_t UsbServerProxy::SetDeviceMessage(MessageParcel &data, uint8_t busNum, uint8_t devAddr)
37 {
38 WRITE_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
39 WRITE_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
40 return UEC_OK;
41 }
42
SetBufferMessage(MessageParcel & data,const std::vector<uint8_t> & bufferData)43 int32_t UsbServerProxy::SetBufferMessage(MessageParcel &data, const std::vector<uint8_t> &bufferData)
44 {
45 uint32_t length = bufferData.size();
46 const uint8_t *ptr = bufferData.data();
47 if (!ptr) {
48 length = 0;
49 }
50
51 if (!data.WriteUint32(length)) {
52 USB_HILOGE(MODULE_USBD, "write length failed:%{public}u", length);
53 return UEC_SERVICE_WRITE_PARCEL_ERROR;
54 }
55 if ((ptr) && (length > 0) && !data.WriteBuffer(reinterpret_cast<const void *>(ptr), length)) {
56 USB_HILOGE(MODULE_USBD, "write buffer failed length:%{public}u", length);
57 return UEC_SERVICE_WRITE_PARCEL_ERROR;
58 }
59
60 USB_HILOGI(MODULE_USBD, "success length:%{public}u", length);
61 return UEC_OK;
62 }
63
GetBufferMessage(MessageParcel & data,std::vector<uint8_t> & bufferData)64 int32_t UsbServerProxy::GetBufferMessage(MessageParcel &data, std::vector<uint8_t> &bufferData)
65 {
66 uint32_t dataSize = 0;
67 bufferData.clear();
68 if (!data.ReadUint32(dataSize)) {
69 USB_HILOGE(MODULE_USBD, "read dataSize failed");
70 return UEC_SERVICE_READ_PARCEL_ERROR;
71 }
72 if (dataSize == 0) {
73 USB_HILOGI(MODULE_USBD, "invalid size:%{public}u", dataSize);
74 return UEC_OK;
75 }
76
77 const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
78 if (readData == nullptr) {
79 USB_HILOGE(MODULE_USBD, "failed size:%{public}u", dataSize);
80 return UEC_SERVICE_READ_PARCEL_ERROR;
81 }
82 std::vector<uint8_t> tdata(readData, readData + dataSize);
83 bufferData.swap(tdata);
84 return UEC_OK;
85 }
86
GetDevices(std::vector<UsbDevice> & deviceList)87 int32_t UsbServerProxy::GetDevices(std::vector<UsbDevice> &deviceList)
88 {
89 int32_t ret;
90 sptr<IRemoteObject> remote = Remote();
91 if (remote == nullptr) {
92 USB_HILOGE(MODULE_USB_INNERKIT, "remote is failed");
93 return ERR_INVALID_VALUE;
94 }
95 MessageParcel data;
96 MessageParcel reply;
97 MessageOption option;
98
99 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
100 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
101 return ERR_INVALID_VALUE;
102 }
103
104 ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_DEVICES), data, reply, option);
105 if (ret != UEC_OK) {
106 USB_HILOGE(MODULE_USB_INNERKIT, "failed code: %{public}d", ret);
107 return ret;
108 }
109 ret = GetDeviceListMessageParcel(reply, deviceList);
110 return ret;
111 }
112
GetDeviceListMessageParcel(MessageParcel & data,std::vector<UsbDevice> & deviceList)113 int32_t UsbServerProxy::GetDeviceListMessageParcel(MessageParcel &data, std::vector<UsbDevice> &deviceList)
114 {
115 int32_t count;
116 READ_PARCEL_WITH_RET(data, Int32, count, UEC_SERVICE_READ_PARCEL_ERROR);
117 if (count > MAX_DEVICE_NUM) {
118 USB_HILOGE(MODULE_USB_INNERKIT, "the maximum number of devices is exceeded!");
119 return ERR_INVALID_VALUE;
120 }
121
122 for (int32_t i = 0; i < count; ++i) {
123 UsbDevice devInfo;
124 GetDeviceMessageParcel(data, devInfo);
125 deviceList.push_back(devInfo);
126 }
127 return UEC_OK;
128 }
129
GetDeviceMessageParcel(MessageParcel & data,UsbDevice & devInfo)130 int32_t UsbServerProxy::GetDeviceMessageParcel(MessageParcel &data, UsbDevice &devInfo)
131 {
132 int32_t tmp;
133 uint8_t tui8;
134 uint16_t tui16;
135 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
136 devInfo.SetBusNum(tmp);
137 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
138 devInfo.SetDevAddr(tmp);
139
140 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
141 devInfo.SetVendorId(tmp);
142 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
143 devInfo.SetProductId(tmp);
144 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
145 devInfo.SetClass(tmp);
146 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
147 devInfo.SetSubclass(tmp);
148 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
149 devInfo.SetProtocol(tmp);
150 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
151 devInfo.SetiManufacturer(tui8);
152 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
153 devInfo.SetiProduct(tui8);
154 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
155 devInfo.SetiSerialNumber(tui8);
156 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
157 devInfo.SetbMaxPacketSize0(tui8);
158 READ_PARCEL_WITH_RET(data, Uint16, tui16, UEC_SERVICE_READ_PARCEL_ERROR);
159 devInfo.SetbcdUSB(tui16);
160 READ_PARCEL_WITH_RET(data, Uint16, tui16, UEC_SERVICE_READ_PARCEL_ERROR);
161 devInfo.SetbcdDevice(tui16);
162 std::u16string tstr;
163 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
164 devInfo.SetName(Str16ToStr8(tstr));
165 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
166 devInfo.SetManufacturerName(Str16ToStr8(tstr));
167 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
168 devInfo.SetProductName(Str16ToStr8(tstr));
169 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
170 devInfo.SetVersion(Str16ToStr8(tstr));
171 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
172 devInfo.SetmSerial(Str16ToStr8(tstr));
173
174 USB_HILOGI(MODULE_USB_INNERKIT, "devName:%{public}s Bus:%{public}d dev:%{public}d ", devInfo.GetName().c_str(),
175 devInfo.GetBusNum(), devInfo.GetDevAddr());
176 std::vector<USBConfig> configs;
177 GetDeviceConfigsMessageParcel(data, configs);
178 devInfo.SetConfigs(configs);
179 return UEC_OK;
180 }
181
GetDeviceConfigsMessageParcel(MessageParcel & data,std::vector<USBConfig> & configs)182 int32_t UsbServerProxy::GetDeviceConfigsMessageParcel(MessageParcel &data, std::vector<USBConfig> &configs)
183 {
184 uint32_t configCount;
185 uint8_t tui8;
186 std::u16string tstr;
187 data.ReadUint32(configCount);
188
189 int32_t tmp;
190 uint32_t attributes;
191 if (configCount > MAX_CONFIG_NUM) {
192 USB_HILOGE(MODULE_USB_SERVICE, "the maximum number of configurations is exceeded!");
193 return ERR_INVALID_VALUE;
194 }
195 for (uint32_t i = 0; i < configCount; ++i) {
196 USBConfig config;
197 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
198 config.SetId(tmp);
199 READ_PARCEL_WITH_RET(data, Uint32, attributes, UEC_SERVICE_READ_PARCEL_ERROR);
200 config.SetAttribute(attributes);
201 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
202 config.SetMaxPower(tmp);
203
204 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
205 config.SetiConfiguration(tui8);
206 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
207 config.SetName(Str16ToStr8(tstr));
208
209 std::vector<UsbInterface> interfaces;
210 if (int32_t ret = GetDeviceInterfacesMessageParcel(data, interfaces); ret != UEC_OK) {
211 USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceInterfacesMessageParcel failed ret:%{public}d", ret);
212 return ret;
213 }
214
215 config.SetInterfaces(interfaces);
216 configs.push_back(config);
217 USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", config.ToString().c_str());
218 }
219
220 return UEC_OK;
221 }
222
GetDeviceInterfacesMessageParcel(MessageParcel & data,std::vector<UsbInterface> & interfaces)223 int32_t UsbServerProxy::GetDeviceInterfacesMessageParcel(MessageParcel &data, std::vector<UsbInterface> &interfaces)
224 {
225 int32_t tmp;
226 int32_t interfaceCount;
227 uint8_t tui8;
228 std::u16string tstr;
229 data.ReadInt32(tmp);
230 interfaceCount = tmp;
231 if (interfaceCount > MAX_INTERFACE_NUM) {
232 USB_HILOGE(MODULE_USB_SERVICE, "the maximum number of interfaces is exceeded!");
233 return ERR_INVALID_VALUE;
234 }
235 for (int32_t i = 0; i < interfaceCount; ++i) {
236 UsbInterface interface;
237 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
238 interface.SetId(tmp);
239 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
240 interface.SetClass(tmp);
241 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
242 interface.SetSubClass(tmp);
243 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
244 interface.SetAlternateSetting(tmp);
245 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
246 interface.SetProtocol(tmp);
247
248 READ_PARCEL_WITH_RET(data, Uint8, tui8, UEC_SERVICE_READ_PARCEL_ERROR);
249 interface.SetiInterface(tui8);
250 READ_PARCEL_WITH_RET(data, String16, tstr, UEC_SERVICE_READ_PARCEL_ERROR);
251 interface.SetName(Str16ToStr8(tstr));
252
253 std::vector<USBEndpoint> eps;
254 if (int32_t ret = GetDeviceEndpointsMessageParcel(data, eps); ret != UEC_OK) {
255 USB_HILOGE(MODULE_USB_SERVICE, "GetDeviceEndpointsMessageParcel failed ret:%{public}d", ret);
256 return ret;
257 }
258
259 for (size_t j = 0; j < eps.size(); ++j) {
260 eps[j].SetInterfaceId(interface.GetId());
261 }
262 interface.SetEndpoints(eps);
263 interfaces.push_back(interface);
264 USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", interface.ToString().c_str());
265 }
266 return UEC_OK;
267 }
268
GetDeviceEndpointsMessageParcel(MessageParcel & data,std::vector<USBEndpoint> & eps)269 int32_t UsbServerProxy::GetDeviceEndpointsMessageParcel(MessageParcel &data, std::vector<USBEndpoint> &eps)
270 {
271 int32_t tmp;
272 int32_t epCount;
273 uint32_t attributes;
274 uint32_t address;
275 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
276 epCount = tmp;
277 if (epCount > MAX_ENDPOINT_NUM) {
278 USB_HILOGE(MODULE_USB_SERVICE, "the maximum number of endpoints is exceeded!");
279 return ERR_INVALID_VALUE;
280 }
281 for (int32_t i = 0; i < epCount; ++i) {
282 USBEndpoint ep;
283 READ_PARCEL_WITH_RET(data, Uint32, address, UEC_SERVICE_READ_PARCEL_ERROR);
284 ep.SetAddr(address);
285 READ_PARCEL_WITH_RET(data, Uint32, attributes, UEC_SERVICE_READ_PARCEL_ERROR);
286 ep.SetAttr(attributes);
287 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
288 ep.SetInterval(tmp);
289 READ_PARCEL_WITH_RET(data, Int32, tmp, UEC_SERVICE_READ_PARCEL_ERROR);
290 ep.SetMaxPacketSize(tmp);
291 eps.push_back(ep);
292 USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", ep.ToString().c_str());
293 }
294 return UEC_OK;
295 }
296
OpenDevice(uint8_t busNum,uint8_t devAddr)297 int32_t UsbServerProxy::OpenDevice(uint8_t busNum, uint8_t devAddr)
298 {
299 MessageParcel data;
300 MessageParcel reply;
301 MessageOption option;
302 sptr<IRemoteObject> remote = Remote();
303 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
304 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
305 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
306 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
307 }
308
309 int32_t ret = SetDeviceMessage(data, busNum, devAddr);
310 if (ret != UEC_OK) {
311 USB_HILOGE(MODULE_USB_INNERKIT, "SetDeviceMessage failed, ret:%{public}d", ret);
312 return ret;
313 }
314
315 ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_OPEN_DEVICE), data, reply, option);
316 if (ret != UEC_OK) {
317 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
318 }
319 return ret;
320 }
321
HasRight(std::string deviceName)322 bool UsbServerProxy::HasRight(std::string deviceName)
323 {
324 MessageParcel data;
325 MessageOption option;
326 MessageParcel reply;
327 sptr<IRemoteObject> remote = Remote();
328 RETURN_IF_WITH_RET(remote == nullptr, false);
329 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
330 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
331 return false;
332 }
333
334 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), false);
335 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_HAS_RIGHT), data, reply, option);
336 if (ret != UEC_OK) {
337 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
338 return false;
339 }
340
341 bool result = false;
342 READ_PARCEL_WITH_RET(reply, Bool, result, false);
343
344 return result;
345 }
346
RequestRight(std::string deviceName)347 int32_t UsbServerProxy::RequestRight(std::string deviceName)
348 {
349 MessageParcel reply;
350 MessageOption option;
351 MessageParcel data;
352 sptr<IRemoteObject> remote = Remote();
353 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
354 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
355 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
356 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
357 }
358 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), UEC_INTERFACE_WRITE_PARCEL_ERROR);
359 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_RIGHT),
360 data, reply, option);
361 if (ret != UEC_OK) {
362 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
363 return ret;
364 }
365 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
366 return ret;
367 }
368
RemoveRight(std::string deviceName)369 int32_t UsbServerProxy::RemoveRight(std::string deviceName)
370 {
371 MessageParcel reply;
372 MessageOption option;
373 MessageParcel data;
374 sptr<IRemoteObject> remote = Remote();
375 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
376 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
377 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
378 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
379 }
380 WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), UEC_INTERFACE_WRITE_PARCEL_ERROR);
381 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REMOVE_RIGHT),
382 data, reply, option);
383 if (ret != UEC_OK) {
384 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
385 return ret;
386 }
387 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
388 return ret;
389 }
390
GetCurrentFunctions(int32_t & funcs)391 int32_t UsbServerProxy::GetCurrentFunctions(int32_t &funcs)
392 {
393 sptr<IRemoteObject> remote = Remote();
394 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
395
396 MessageParcel data;
397 MessageParcel reply;
398 MessageOption option;
399
400 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
401 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
402 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
403 }
404
405 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_CURRENT_FUNCTIONS),
406 data, reply, option);
407 if (ret != UEC_OK) {
408 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
409 return ret;
410 }
411 READ_PARCEL_WITH_RET(reply, Int32, funcs, UEC_INTERFACE_READ_PARCEL_ERROR);
412 return ret;
413 }
414
SetCurrentFunctions(int32_t funcs)415 int32_t UsbServerProxy::SetCurrentFunctions(int32_t funcs)
416 {
417 sptr<IRemoteObject> remote = Remote();
418 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
419
420 MessageOption option;
421 MessageParcel data;
422 MessageParcel reply;
423
424 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
425 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
426 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
427 }
428 WRITE_PARCEL_WITH_RET(data, Int32, funcs, UEC_INTERFACE_WRITE_PARCEL_ERROR);
429 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_CURRENT_FUNCTIONS),
430 data, reply, option);
431 if (ret != UEC_OK) {
432 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
433 }
434 return ret;
435 }
436
UsbFunctionsFromString(std::string_view funcs)437 int32_t UsbServerProxy::UsbFunctionsFromString(std::string_view funcs)
438 {
439 sptr<IRemoteObject> remote = Remote();
440 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
441 MessageOption option;
442 MessageParcel data;
443 MessageParcel reply;
444
445 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
446 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
447 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
448 }
449 WRITE_PARCEL_WITH_RET(data, String, std::string {funcs}, UEC_INTERFACE_WRITE_PARCEL_ERROR);
450 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_FROM_STRING),
451 data, reply, option);
452 if (ret != UEC_OK) {
453 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
454 return UEC_INTERFACE_INVALID_VALUE;
455 }
456 int32_t result = 0;
457 READ_PARCEL_WITH_RET(reply, Int32, result, INVALID_USB_INT_VALUE);
458 return result;
459 }
460
UsbFunctionsToString(int32_t funcs)461 std::string UsbServerProxy::UsbFunctionsToString(int32_t funcs)
462 {
463 sptr<IRemoteObject> remote = Remote();
464
465 MessageParcel data;
466 MessageOption option;
467 MessageParcel reply;
468
469 RETURN_IF_WITH_RET(remote == nullptr, INVALID_STRING_VALUE);
470
471 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
472 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
473 return INVALID_STRING_VALUE;
474 }
475 WRITE_PARCEL_WITH_RET(data, Int32, funcs, INVALID_STRING_VALUE);
476 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_TO_STRING),
477 data, reply, option);
478 if (ret != UEC_OK) {
479 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
480 return INVALID_STRING_VALUE;
481 }
482 std::string result;
483 READ_PARCEL_WITH_RET(reply, String, result, INVALID_STRING_VALUE);
484 return result;
485 }
486
GetPorts(std::vector<UsbPort> & ports)487 int32_t UsbServerProxy::GetPorts(std::vector<UsbPort> &ports)
488 {
489 MessageOption option;
490 sptr<IRemoteObject> remote = Remote();
491
492 MessageParcel data;
493 MessageParcel reply;
494 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
495 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
496 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
497 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
498 }
499 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_PORTS), data, reply, option);
500 if (ret != UEC_OK) {
501 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
502 return ret;
503 }
504 int32_t size;
505 READ_PARCEL_WITH_RET(reply, Int32, size, UEC_INTERFACE_READ_PARCEL_ERROR);
506 USB_HILOGI(MODULE_USB_INNERKIT, "GetPorts size %{public}d", size);
507 if (size > MAX_PORT_NUM) {
508 USB_HILOGE(MODULE_INNERKIT, "the maximum number of ports is exceeded!");
509 return ERR_INVALID_VALUE;
510 }
511 for (int32_t i = 0; i < size; ++i) {
512 USB_HILOGI(MODULE_USB_INNERKIT, "ParseUsbPort : %{public}d", i);
513 ret = ParseUsbPort(reply, ports);
514 if (ret) {
515 return ret;
516 }
517 }
518 return ret;
519 }
520
ParseUsbPort(MessageParcel & reply,std::vector<UsbPort> & ports)521 int32_t UsbServerProxy::ParseUsbPort(MessageParcel &reply, std::vector<UsbPort> &ports)
522 {
523 UsbPort port;
524 UsbPortStatus status;
525 READ_PARCEL_WITH_RET(reply, Int32, port.id, UEC_INTERFACE_READ_PARCEL_ERROR);
526 USB_HILOGI(MODULE_USB_INNERKIT, "UsbServerProxy::port->id %{public}d", port.id);
527 port.supportedModes = reply.ReadInt32();
528 status.currentMode = reply.ReadInt32();
529 status.currentPowerRole = reply.ReadInt32();
530 status.currentDataRole = reply.ReadInt32();
531 port.usbPortStatus = status;
532 USB_HILOGI(MODULE_USB_INNERKIT, "UsbServerProxy::port.usbPortStatus.currentMode %{public}d",
533 port.usbPortStatus.currentMode);
534 ports.push_back(port);
535 return UEC_OK;
536 }
537
GetSupportedModes(int32_t portId,int32_t & supportedModes)538 int32_t UsbServerProxy::GetSupportedModes(int32_t portId, int32_t &supportedModes)
539 {
540 MessageParcel data;
541 MessageParcel reply;
542 MessageOption option;
543 sptr<IRemoteObject> remote = Remote();
544 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
545 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
546 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
547 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
548 }
549 WRITE_PARCEL_WITH_RET(data, Int32, portId, UEC_INTERFACE_WRITE_PARCEL_ERROR);
550 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_SUPPORTED_MODES),
551 data, reply, option);
552 if (ret) {
553 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
554 return ret;
555 }
556 READ_PARCEL_WITH_RET(reply, Int32, supportedModes, UEC_INTERFACE_READ_PARCEL_ERROR);
557 return ret;
558 }
559
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)560 int32_t UsbServerProxy::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
561 {
562 MessageParcel data;
563 MessageParcel reply;
564 MessageOption option;
565 sptr<IRemoteObject> remote = Remote();
566 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
567 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
568 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
569 return UEC_INTERFACE_WRITE_PARCEL_ERROR;
570 }
571 WRITE_PARCEL_WITH_RET(data, Int32, portId, UEC_INTERFACE_WRITE_PARCEL_ERROR);
572 WRITE_PARCEL_WITH_RET(data, Int32, powerRole, UEC_INTERFACE_WRITE_PARCEL_ERROR);
573 WRITE_PARCEL_WITH_RET(data, Int32, dataRole, UEC_INTERFACE_WRITE_PARCEL_ERROR);
574 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_PORT_ROLE),
575 data, reply, option);
576 if (ret) {
577 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
578 return ret;
579 }
580 return ret;
581 }
582
ClaimInterface(uint8_t busNum,uint8_t devAddr,uint8_t interface,uint8_t force)583 int32_t UsbServerProxy::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t force)
584 {
585 sptr<IRemoteObject> remote = Remote();
586 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
587 MessageParcel data;
588 MessageParcel reply;
589 MessageOption option;
590 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
591 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
592 return ERR_ENOUGH_DATA;
593 }
594 SetDeviceMessage(data, busNum, devAddr);
595 WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
596 WRITE_PARCEL_WITH_RET(data, Uint8, force, UEC_SERVICE_WRITE_PARCEL_ERROR);
597 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_CLAIM_INTERFACE),
598 data, reply, option);
599 if (ret != UEC_OK) {
600 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
601 return ret;
602 }
603 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
604 return ret;
605 }
606
ReleaseInterface(uint8_t busNum,uint8_t devAddr,uint8_t interface)607 int32_t UsbServerProxy::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface)
608 {
609 sptr<IRemoteObject> remote = Remote();
610 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
611 MessageParcel data;
612 MessageParcel reply;
613 MessageOption option;
614 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
615 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
616 return ERR_ENOUGH_DATA;
617 }
618 SetDeviceMessage(data, busNum, devAddr);
619 WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
620 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_RELEASE_INTERFACE),
621 data, reply, option);
622 if (ret != UEC_OK) {
623 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
624 return ret;
625 }
626 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
627 return ret;
628 }
BulkTransferRead(const UsbDev & dev,const UsbPipe & pipe,std::vector<uint8_t> & bufferData,int32_t timeOut)629 int32_t UsbServerProxy::BulkTransferRead(
630 const UsbDev &dev, const UsbPipe &pipe, std::vector<uint8_t> &bufferData, int32_t timeOut)
631 {
632 sptr<IRemoteObject> remote = Remote();
633 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
634 MessageParcel data;
635 MessageParcel reply;
636 MessageOption option;
637 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
638 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
639 return ERR_ENOUGH_DATA;
640 }
641 SetDeviceMessage(data, dev.busNum, dev.devAddr);
642 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
643 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
644 WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
645 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ),
646 data, reply, option);
647 if (ret != UEC_OK) {
648 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
649 return ret;
650 }
651 ret = GetBufferMessage(reply, bufferData);
652 if (ret != UEC_OK) {
653 USB_HILOGE(MODULE_USB_INNERKIT, "get buffer is failed, error code: %{public}d", ret);
654 return ret;
655 }
656 USB_HILOGI(MODULE_USBD, "Set buffer message. length = %{public}zu", bufferData.size());
657 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
658 return ret;
659 }
660
BulkTransferReadwithLength(const UsbDev & dev,const UsbPipe & pipe,int32_t length,std::vector<uint8_t> & bufferData,int32_t timeOut)661 int32_t UsbServerProxy::BulkTransferReadwithLength(const UsbDev &dev, const UsbPipe &pipe,
662 int32_t length, std::vector<uint8_t> &bufferData, int32_t timeOut)
663 {
664 sptr<IRemoteObject> remote = Remote();
665 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
666 MessageParcel data;
667 MessageParcel reply;
668 MessageOption option;
669 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
670 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
671 return ERR_ENOUGH_DATA;
672 }
673 SetDeviceMessage(data, dev.busNum, dev.devAddr);
674 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
675 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
676 WRITE_PARCEL_WITH_RET(data, Int32, length, UEC_SERVICE_WRITE_PARCEL_ERROR);
677 WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
678 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ_WITH_LENGTH),
679 data, reply, option);
680 if (ret != UEC_OK) {
681 USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
682 return ret;
683 }
684 ret = GetBufferMessage(reply, bufferData);
685 if (ret != UEC_OK) {
686 USB_HILOGE(MODULE_USB_INNERKIT, "get buffer is failed, error code: %{public}d", ret);
687 return ret;
688 }
689 USB_HILOGI(MODULE_USBD, "Set buffer message. length = %{public}zu", bufferData.size());
690 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
691 return ret;
692 }
693
BulkTransferWrite(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & bufferData,int32_t timeOut)694 int32_t UsbServerProxy::BulkTransferWrite(
695 const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData, int32_t timeOut)
696 {
697 sptr<IRemoteObject> remote = Remote();
698 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
699 MessageParcel data;
700 MessageParcel reply;
701 MessageOption option;
702 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
703 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
704 return ERR_ENOUGH_DATA;
705 }
706 SetDeviceMessage(data, dev.busNum, dev.devAddr);
707 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
708 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
709 WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
710 int32_t ret = SetBufferMessage(data, bufferData);
711 if (UEC_OK != ret) {
712 USB_HILOGE(MODULE_INNERKIT, "SetBufferMessage ret:%{public}d", ret);
713 return ret;
714 }
715 ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_WRITE),
716 data, reply, option);
717 if (UEC_OK != ret) {
718 USB_HILOGE(MODULE_INNERKIT, "SendRequest ret:%{public}d", ret);
719 return ret;
720 }
721 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
722 return ret;
723 }
724
ControlTransfer(const UsbDev & dev,const UsbCtrlTransfer & ctrl,std::vector<uint8_t> & bufferData)725 int32_t UsbServerProxy::ControlTransfer(
726 const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
727 {
728 sptr<IRemoteObject> remote = Remote();
729 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
730 MessageParcel data;
731 MessageParcel reply;
732 MessageOption option;
733 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
734 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
735 return UEC_SERVICE_INNER_ERR;
736 }
737 SetDeviceMessage(data, dev.busNum, dev.devAddr);
738 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
739 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestCmd, UEC_SERVICE_WRITE_PARCEL_ERROR);
740 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.value, UEC_SERVICE_WRITE_PARCEL_ERROR);
741 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.index, UEC_SERVICE_WRITE_PARCEL_ERROR);
742 WRITE_PARCEL_WITH_RET(data, Int32, ctrl.timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
743 int32_t ret = SetBufferMessage(data, bufferData);
744 if (UEC_OK != ret) {
745 USB_HILOGE(MODULE_INNERKIT, "write failed! len:%{public}d", ret);
746 return ret;
747 }
748
749 uint32_t reqType = static_cast<uint32_t>(ctrl.requestType);
750 bool isWrite = ((reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
751 ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_CONTROL_TRANSFER), data, reply, option);
752 if (ret != UEC_OK) {
753 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_CONTROL_TRANSFER ret:%{public}d", ret);
754 return ret;
755 }
756 if (!isWrite) {
757 ret = GetBufferMessage(reply, bufferData);
758 if (UEC_OK != ret) {
759 USB_HILOGE(MODULE_USBD, "Get buffer message error. ret = %{public}d", ret);
760 return ret;
761 }
762 USB_HILOGI(MODULE_USBD, "Get buffer message. length = %{public}zu", bufferData.size());
763 }
764 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
765 return ret;
766 }
767
UsbControlTransfer(const UsbDev & dev,const UsbCtrlTransferParams & ctrlParams,std::vector<uint8_t> & bufferData)768 int32_t UsbServerProxy::UsbControlTransfer(
769 const UsbDev &dev, const UsbCtrlTransferParams &ctrlParams, std::vector<uint8_t> &bufferData)
770 {
771 sptr<IRemoteObject> remote = Remote();
772 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
773 MessageParcel data;
774 MessageParcel reply;
775 MessageOption option;
776 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
777 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
778 return UEC_SERVICE_INNER_ERR;
779 }
780 SetDeviceMessage(data, dev.busNum, dev.devAddr);
781 WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
782 WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.requestCmd, UEC_SERVICE_WRITE_PARCEL_ERROR);
783 WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.value, UEC_SERVICE_WRITE_PARCEL_ERROR);
784 WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.index, UEC_SERVICE_WRITE_PARCEL_ERROR);
785 WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.length, UEC_SERVICE_WRITE_PARCEL_ERROR);
786 WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
787 int32_t ret = SetBufferMessage(data, bufferData);
788 if (UEC_OK != ret) {
789 USB_HILOGE(MODULE_INNERKIT, "write failed! len:%{public}d", ret);
790 return ret;
791 }
792
793 uint32_t reqType = static_cast<uint32_t>(ctrlParams.requestType);
794 bool isWrite = ((reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
795 ret = remote->SendRequest(
796 static_cast<int32_t>(UsbInterfaceCode::USB_FUN_USB_CONTROL_TRANSFER), data, reply, option);
797 if (ret != UEC_OK) {
798 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_USB_CONTROL_TRANSFER ret:%{public}d", ret);
799 return ret;
800 }
801 if (!isWrite) {
802 ret = GetBufferMessage(reply, bufferData);
803 if (UEC_OK != ret) {
804 USB_HILOGE(MODULE_USBD, "Get buffer message error. ret = %{public}d", ret);
805 return ret;
806 }
807 USB_HILOGI(MODULE_USBD, "Get buffer message. length = %{public}zu", bufferData.size());
808 }
809 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
810 return ret;
811 }
812
SetActiveConfig(uint8_t busNum,uint8_t devAddr,uint8_t configIndex)813 int32_t UsbServerProxy::SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex)
814 {
815 sptr<IRemoteObject> remote = Remote();
816 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
817 MessageParcel data;
818 MessageParcel reply;
819 MessageOption option;
820 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
821 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
822 return ERR_ENOUGH_DATA;
823 }
824 SetDeviceMessage(data, busNum, devAddr);
825 WRITE_PARCEL_WITH_RET(data, Uint8, configIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
826 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_ACTIVE_CONFIG),
827 data, reply, option);
828 if (UEC_OK != ret) {
829 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_SET_ACTIVE_CONFIG ret:%{public}d", ret);
830 return ret;
831 }
832 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
833 return ret;
834 }
GetActiveConfig(uint8_t busNum,uint8_t devAddr,uint8_t & configIndex)835 int32_t UsbServerProxy::GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex)
836 {
837 sptr<IRemoteObject> remote = Remote();
838 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
839 MessageParcel data;
840 MessageParcel reply;
841 MessageOption option;
842 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
843 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
844 return ERR_ENOUGH_DATA;
845 }
846 SetDeviceMessage(data, busNum, devAddr);
847 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_ACTIVE_CONFIG),
848 data, reply, option);
849 if (ret != UEC_OK) {
850 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_GET_ACTIVE_CONFIG ret:%{public}d", ret);
851 return ret;
852 }
853 READ_PARCEL_WITH_RET(reply, Uint8, configIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
854 return ret;
855 }
SetInterface(uint8_t busNum,uint8_t devAddr,uint8_t interfaceid,uint8_t altIndex)856 int32_t UsbServerProxy::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex)
857 {
858 sptr<IRemoteObject> remote = Remote();
859 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
860 MessageParcel data;
861 MessageParcel reply;
862 MessageOption option;
863 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
864 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
865 return ERR_ENOUGH_DATA;
866 }
867 SetDeviceMessage(data, busNum, devAddr);
868 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
869 WRITE_PARCEL_WITH_RET(data, Uint8, altIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
870 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_INTERFACE),
871 data, reply, option);
872 if (UEC_OK != ret) {
873 USB_HILOGE(MODULE_INNERKIT, "USB_FUN_SET_INTERFACE ret:%{public}d", ret);
874 return ret;
875 }
876 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
877 return ret;
878 }
GetRawDescriptor(uint8_t busNum,uint8_t devAddr,std::vector<uint8_t> & bufferData)879 int32_t UsbServerProxy::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData)
880 {
881 sptr<IRemoteObject> remote = Remote();
882 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
883 MessageParcel data;
884 MessageParcel reply;
885 MessageOption option;
886 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
887 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
888 return ERR_ENOUGH_DATA;
889 }
890 SetDeviceMessage(data, busNum, devAddr);
891 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_DESCRIPTOR),
892 data, reply, option);
893 if (ret == UEC_OK) {
894 ret = GetBufferMessage(reply, bufferData);
895 if (UEC_OK != ret) {
896 USB_HILOGE(MODULE_INNERKIT, "get failed ret:%{public}d", ret);
897 }
898 }
899 return ret;
900 }
901
GetFileDescriptor(uint8_t busNum,uint8_t devAddr,int32_t & fd)902 int32_t UsbServerProxy::GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd)
903 {
904 sptr<IRemoteObject> remote = Remote();
905 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
906 MessageParcel data;
907 MessageParcel reply;
908 MessageOption option;
909 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
910 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
911 return ERR_ENOUGH_DATA;
912 }
913 SetDeviceMessage(data, busNum, devAddr);
914 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_FILEDESCRIPTOR),
915 data, reply, option);
916 if (ret == UEC_OK) {
917 fd = -1;
918 if (!ReadFileDescriptor(reply, fd)) {
919 USB_HILOGW(MODULE_USB_SERVICE, "%{public}s: read fd failed!", __func__);
920 return UEC_INTERFACE_READ_PARCEL_ERROR;
921 }
922 }
923 return ret;
924 }
925
RequestQueue(const UsbDev & dev,const UsbPipe & pipe,const std::vector<uint8_t> & clientData,const std::vector<uint8_t> & bufferData)926 int32_t UsbServerProxy::RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
927 const std::vector<uint8_t> &bufferData)
928 {
929 sptr<IRemoteObject> remote = Remote();
930 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
931 MessageParcel data;
932 MessageParcel reply;
933 MessageOption option;
934 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
935 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
936 return ERR_ENOUGH_DATA;
937 }
938 SetDeviceMessage(data, dev.busNum, dev.devAddr);
939 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
940 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
941
942 int32_t ret = UsbServerProxy::SetBufferMessage(data, clientData);
943 if (UEC_OK != ret) {
944 USB_HILOGE(MODULE_INNERKIT, "set clientData failed ret:%{public}d", ret);
945 return ERR_INVALID_VALUE;
946 }
947
948 ret = UsbServerProxy::SetBufferMessage(data, bufferData);
949 if (UEC_OK != ret) {
950 USB_HILOGE(MODULE_INNERKIT, "setBuffer failed ret:%{public}d", ret);
951 return ERR_INVALID_VALUE;
952 }
953
954 ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_QUEUE), data, reply, option);
955 if (ret != UEC_OK) {
956 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
957 return ret;
958 }
959 return ret;
960 }
961
RequestWait(const UsbDev & dev,int32_t timeOut,std::vector<uint8_t> & clientData,std::vector<uint8_t> & bufferData)962 int32_t UsbServerProxy::RequestWait(
963 const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData, std::vector<uint8_t> &bufferData)
964 {
965 sptr<IRemoteObject> remote = Remote();
966 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
967 MessageParcel data;
968 MessageParcel reply;
969 MessageOption option;
970
971 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
972 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
973 return ERR_ENOUGH_DATA;
974 }
975
976 SetDeviceMessage(data, dev.busNum, dev.devAddr);
977 WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
978 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_WAIT),
979 data, reply, option);
980 if (ret != UEC_OK) {
981 USB_HILOGE(MODULE_INNERKIT, "queue failed! ret:%{public}d", ret);
982 return ret;
983 }
984
985 ret = UsbServerProxy::GetBufferMessage(reply, clientData);
986 if (ret != UEC_OK) {
987 USB_HILOGE(MODULE_INNERKIT, "get clientData failed! ret:%{public}d", ret);
988 return ret;
989 }
990
991 ret = UsbServerProxy::GetBufferMessage(reply, bufferData);
992 if (ret != UEC_OK) {
993 USB_HILOGE(MODULE_INNERKIT, "get buffer failed! ret:%{public}d", ret);
994 return ret;
995 }
996
997 return ret;
998 }
999
RequestCancel(uint8_t busNum,uint8_t devAddr,uint8_t interfaceid,uint8_t endpointId)1000 int32_t UsbServerProxy::RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId)
1001 {
1002 int32_t ret;
1003 sptr<IRemoteObject> remote = Remote();
1004 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1005 MessageParcel data;
1006 MessageParcel reply;
1007 MessageOption option;
1008 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1009 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
1010 return ERR_ENOUGH_DATA;
1011 }
1012
1013 SetDeviceMessage(data, busNum, devAddr);
1014 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
1015 WRITE_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1016 ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_CANCEL), data, reply, option);
1017 if (ret != UEC_OK) {
1018 USB_HILOGE(MODULE_INNERKIT, "request cancel failed!");
1019 }
1020
1021 return ret;
1022 }
1023
Close(uint8_t busNum,uint8_t devAddr)1024 int32_t UsbServerProxy::Close(uint8_t busNum, uint8_t devAddr)
1025 {
1026 sptr<IRemoteObject> remote = Remote();
1027 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1028 MessageOption option;
1029 MessageParcel data;
1030 MessageParcel reply;
1031 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1032 USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
1033 return ERR_ENOUGH_DATA;
1034 }
1035
1036 SetDeviceMessage(data, busNum, devAddr);
1037 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_CLOSE_DEVICE),
1038 data, reply, option);
1039 if (ret != UEC_OK) {
1040 USB_HILOGE(MODULE_INNERKIT, "queue failed!");
1041 return ret;
1042 }
1043 READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
1044 return ret;
1045 }
1046
RegBulkCallback(const UsbDev & dev,const UsbPipe & pipe,const sptr<IRemoteObject> & cb)1047 int32_t UsbServerProxy::RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IRemoteObject> &cb)
1048 {
1049 sptr<IRemoteObject> remote = Remote();
1050 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1051 MessageParcel data;
1052 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1053 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1054 return ERR_ENOUGH_DATA;
1055 }
1056 SetDeviceMessage(data, dev.busNum, dev.devAddr);
1057 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1058 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1059 WRITE_PARCEL_WITH_RET(data, RemoteObject, cb, UEC_SERVICE_WRITE_PARCEL_ERROR);
1060 MessageOption option;
1061 MessageParcel reply;
1062 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REG_BULK_CALLBACK),
1063 data, reply, option);
1064 if (ret != UEC_OK) {
1065 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1066 return ret;
1067 }
1068 return ret;
1069 }
1070
UnRegBulkCallback(const UsbDev & dev,const UsbPipe & pipe)1071 int32_t UsbServerProxy::UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe)
1072 {
1073 sptr<IRemoteObject> remote = Remote();
1074 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1075 MessageParcel data;
1076 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1077 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1078 return ERR_ENOUGH_DATA;
1079 }
1080 SetDeviceMessage(data, dev.busNum, dev.devAddr);
1081 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1082 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1083 MessageOption option;
1084 MessageParcel reply;
1085 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_UNREG_BULK_CALLBACK),
1086 data, reply, option);
1087 if (ret != UEC_OK) {
1088 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1089 return ret;
1090 }
1091 return ret;
1092 }
1093
BulkRead(const UsbDev & dev,const UsbPipe & pipe,sptr<Ashmem> & ashmem)1094 int32_t UsbServerProxy::BulkRead(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
1095 {
1096 sptr<IRemoteObject> remote = Remote();
1097 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1098 MessageParcel data;
1099 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1100 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1101 return ERR_ENOUGH_DATA;
1102 }
1103 SetDeviceMessage(data, dev.busNum, dev.devAddr);
1104 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1105 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1106 WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
1107 MessageOption option;
1108 MessageParcel reply;
1109 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_READ),
1110 data, reply, option);
1111 if (ret != UEC_OK) {
1112 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1113 return ret;
1114 }
1115 return ret;
1116 }
1117
BulkWrite(const UsbDev & dev,const UsbPipe & pipe,sptr<Ashmem> & ashmem)1118 int32_t UsbServerProxy::BulkWrite(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
1119 {
1120 sptr<IRemoteObject> remote = Remote();
1121 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1122 MessageParcel data;
1123 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1124 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1125 return ERR_ENOUGH_DATA;
1126 }
1127 SetDeviceMessage(data, dev.busNum, dev.devAddr);
1128 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1129 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1130 WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
1131 MessageOption option;
1132 MessageParcel reply;
1133 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_WRITE),
1134 data, reply, option);
1135 if (ret != UEC_OK) {
1136 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1137 return ret;
1138 }
1139 return ret;
1140 }
1141
BulkCancel(const UsbDev & dev,const UsbPipe & pipe)1142 int32_t UsbServerProxy::BulkCancel(const UsbDev &dev, const UsbPipe &pipe)
1143 {
1144 sptr<IRemoteObject> remote = Remote();
1145 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1146 MessageParcel data;
1147 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1148 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1149 return ERR_ENOUGH_DATA;
1150 }
1151 SetDeviceMessage(data, dev.busNum, dev.devAddr);
1152 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1153 WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1154 MessageOption option;
1155 MessageParcel reply;
1156 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_CANCEL),
1157 data, reply, option);
1158 if (ret != UEC_OK) {
1159 USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1160 return ret;
1161 }
1162 return ret;
1163 }
1164
AddRight(const std::string & bundleName,const std::string & deviceName)1165 int32_t UsbServerProxy::AddRight(const std::string &bundleName, const std::string &deviceName)
1166 {
1167 sptr<IRemoteObject> remote = Remote();
1168 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1169
1170 MessageParcel data;
1171 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1172 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1173 return ERR_ENOUGH_DATA;
1174 }
1175 WRITE_PARCEL_WITH_RET(data, String, bundleName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1176 WRITE_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1177
1178 MessageOption option;
1179 MessageParcel reply;
1180 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_ADD_RIGHT), data, reply, option);
1181 if (ret != UEC_OK) {
1182 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1183 }
1184 return ret;
1185 }
1186
AddAccessRight(const std::string & tokenId,const std::string & deviceName)1187 int32_t UsbServerProxy::AddAccessRight(const std::string &tokenId, const std::string &deviceName)
1188 {
1189 sptr<IRemoteObject> remote = Remote();
1190 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1191
1192 MessageParcel data;
1193 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1194 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1195 return ERR_ENOUGH_DATA;
1196 }
1197 WRITE_PARCEL_WITH_RET(data, String, tokenId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1198 WRITE_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1199
1200 MessageOption option;
1201 MessageParcel reply;
1202 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_ADD_ACCESS_RIGHT),
1203 data, reply, option);
1204 if (ret != UEC_OK) {
1205 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1206 }
1207 return ret;
1208 }
1209
ManageGlobalInterface(bool disable)1210 int32_t UsbServerProxy::ManageGlobalInterface(bool disable)
1211 {
1212 sptr<IRemoteObject> remote = Remote();
1213 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1214
1215 MessageParcel data;
1216 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1217 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1218 return ERR_ENOUGH_DATA;
1219 }
1220 WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR);
1221
1222 MessageOption option;
1223 MessageParcel reply;
1224 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DISABLE_GLOBAL_INTERFACE),
1225 data, reply, option);
1226 if (ret != UEC_OK) {
1227 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1228 }
1229 return ret;
1230 }
1231
ManageDevice(int32_t vendorId,int32_t productId,bool disable)1232 int32_t UsbServerProxy::ManageDevice(int32_t vendorId, int32_t productId, bool disable)
1233 {
1234 sptr<IRemoteObject> remote = Remote();
1235 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1236
1237 MessageParcel data;
1238 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1239 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1240 return ERR_ENOUGH_DATA;
1241 }
1242 WRITE_PARCEL_WITH_RET(data, Int32, vendorId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1243 WRITE_PARCEL_WITH_RET(data, Int32, productId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1244 WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR);
1245
1246 MessageOption option;
1247 MessageParcel reply;
1248 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DISABLE_DEVICE),
1249 data, reply, option);
1250 if (ret != UEC_OK) {
1251 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1252 }
1253 return ret;
1254 }
1255
ManageInterfaceStorage(InterfaceType interfaceType,bool disable)1256 int32_t UsbServerProxy::ManageInterfaceStorage(InterfaceType interfaceType, bool disable)
1257 {
1258 sptr<IRemoteObject> remote = Remote();
1259 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1260
1261 MessageParcel data;
1262 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1263 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1264 return ERR_ENOUGH_DATA;
1265 }
1266 int32_t ifaceType = (int32_t)interfaceType;
1267 WRITE_PARCEL_WITH_RET(data, Int32, ifaceType, UEC_SERVICE_WRITE_PARCEL_ERROR);
1268 WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR);
1269
1270 MessageOption option;
1271 MessageParcel reply;
1272 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_STORAGE),
1273 data, reply, option);
1274 if (ret != UEC_OK) {
1275 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1276 }
1277 return ret;
1278 }
1279
ManageInterfaceType(const std::vector<UsbDeviceType> & disableType,bool disable)1280 int32_t UsbServerProxy::ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable)
1281 {
1282 sptr<IRemoteObject> remote = Remote();
1283 RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1284
1285 MessageParcel data;
1286 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1287 USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1288 return ERR_ENOUGH_DATA;
1289 }
1290 int32_t size = (int32_t)disableType.size();
1291 WRITE_PARCEL_WITH_RET(data, Int32, size, UEC_SERVICE_WRITE_PARCEL_ERROR);
1292
1293 for (const auto &type : disableType) {
1294 WRITE_PARCEL_WITH_RET(data, Int32, type.baseClass, UEC_SERVICE_WRITE_PARCEL_ERROR);
1295 WRITE_PARCEL_WITH_RET(data, Int32, type.subClass, UEC_SERVICE_WRITE_PARCEL_ERROR);
1296 WRITE_PARCEL_WITH_RET(data, Int32, type.protocol, UEC_SERVICE_WRITE_PARCEL_ERROR);
1297 WRITE_PARCEL_WITH_RET(data, Bool, type.isDeviceType, UEC_SERVICE_WRITE_PARCEL_ERROR);
1298 }
1299 WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR);
1300
1301 MessageOption option;
1302 MessageParcel reply;
1303 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_TYPE),
1304 data, reply, option);
1305 if (ret != UEC_OK) {
1306 USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1307 }
1308 return ret;
1309 }
1310
GetDeviceSpeed(uint8_t busNum,uint8_t devAddr,uint8_t & speed)1311 int32_t UsbServerProxy::GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed)
1312 {
1313 sptr<IRemoteObject> remote = Remote();
1314 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1315 MessageParcel data;
1316 MessageParcel reply;
1317 MessageOption option;
1318 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1319 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1320 return ERR_ENOUGH_DATA;
1321 }
1322 SetDeviceMessage(data, busNum, devAddr);
1323 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_DEVICE_SPEED),
1324 data, reply, option);
1325 if (ret == UEC_OK) {
1326 READ_PARCEL_WITH_RET(reply, Uint8, speed, UEC_INTERFACE_READ_PARCEL_ERROR);
1327 }
1328 USB_HILOGE(MODULE_INNERKIT, "GetDeviceSpeed speed:%{public}u", speed);
1329 return ret;
1330 }
1331
GetInterfaceActiveStatus(uint8_t busNum,uint8_t devAddr,uint8_t interfaceid,bool & unactivated)1332 int32_t UsbServerProxy::GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr,
1333 uint8_t interfaceid, bool &unactivated)
1334 {
1335 sptr<IRemoteObject> remote = Remote();
1336 RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1337 MessageParcel data;
1338 MessageParcel reply;
1339 MessageOption option;
1340 if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1341 USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1342 return ERR_ENOUGH_DATA;
1343 }
1344 SetDeviceMessage(data, busNum, devAddr);
1345 WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
1346 int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_DRIVER_ACTIVE_STATUS),
1347 data, reply, option);
1348 if (ret == UEC_OK) {
1349 READ_PARCEL_WITH_RET(reply, Bool, unactivated, UEC_INTERFACE_READ_PARCEL_ERROR);
1350 }
1351 return ret;
1352 }
ReadFileDescriptor(MessageParcel & data,int & fd)1353 bool UsbServerProxy::ReadFileDescriptor(MessageParcel &data, int &fd)
1354 {
1355 fd = -1;
1356 bool fdValid = false;
1357 if (!data.ReadBool(fdValid)) {
1358 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to read fdValid", __func__);
1359 return false;
1360 }
1361
1362 if (fdValid) {
1363 fd = data.ReadFileDescriptor();
1364 if (fd < 0) {
1365 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to read fd", __func__);
1366 return false;
1367 }
1368 }
1369 return true;
1370 }
1371 } // namespace USB
1372 } // namespace OHOS
1373