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