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 
ResetDevice(uint8_t busNum, uint8_t devAddr)322 int32_t UsbServerProxy::ResetDevice(uint8_t busNum, uint8_t devAddr)
323 {
324     sptr<IRemoteObject> remote = Remote();
325     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
326     MessageParcel data;
327     MessageParcel reply;
328     MessageOption option;
329     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
330         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
331         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
332     }
333 
334     int32_t ret = SetDeviceMessage(data, busNum, devAddr);
335     if (ret != UEC_OK) {
336         USB_HILOGE(MODULE_USB_INNERKIT, "SetDeviceMessage failed, ret:%{public}d", ret);
337         return ret;
338     }
339 
340     ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_RESET_DEVICE), data, reply, option);
341     if (ret != UEC_OK) {
342         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
343     }
344     return ret;
345 }
346 
HasRight(std::string deviceName)347 bool UsbServerProxy::HasRight(std::string deviceName)
348 {
349     MessageParcel data;
350     MessageOption option;
351     MessageParcel reply;
352     sptr<IRemoteObject> remote = Remote();
353     RETURN_IF_WITH_RET(remote == nullptr, false);
354     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
355         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
356         return false;
357     }
358 
359     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), false);
360     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_HAS_RIGHT), data, reply, option);
361     if (ret != UEC_OK) {
362         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
363         return false;
364     }
365 
366     bool result = false;
367     READ_PARCEL_WITH_RET(reply, Bool, result, false);
368 
369     return result;
370 }
371 
RequestRight(std::string deviceName)372 int32_t UsbServerProxy::RequestRight(std::string deviceName)
373 {
374     MessageParcel reply;
375     MessageOption option;
376     MessageParcel data;
377     sptr<IRemoteObject> remote = Remote();
378     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
379     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
380         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
381         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
382     }
383     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), UEC_INTERFACE_WRITE_PARCEL_ERROR);
384     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_RIGHT),
385         data, reply, option);
386     if (ret != UEC_OK) {
387         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
388         return ret;
389     }
390     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
391     return ret;
392 }
393 
RemoveRight(std::string deviceName)394 int32_t UsbServerProxy::RemoveRight(std::string deviceName)
395 {
396     MessageParcel reply;
397     MessageOption option;
398     MessageParcel data;
399     sptr<IRemoteObject> remote = Remote();
400     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
401     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
402         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
403         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
404     }
405     WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(deviceName), UEC_INTERFACE_WRITE_PARCEL_ERROR);
406     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REMOVE_RIGHT),
407         data, reply, option);
408     if (ret != UEC_OK) {
409         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
410         return ret;
411     }
412     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
413     return ret;
414 }
415 
GetCurrentFunctions(int32_t &funcs)416 int32_t UsbServerProxy::GetCurrentFunctions(int32_t &funcs)
417 {
418     sptr<IRemoteObject> remote = Remote();
419     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
420 
421     MessageParcel data;
422     MessageParcel reply;
423     MessageOption option;
424 
425     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
426         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
427         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
428     }
429 
430     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_CURRENT_FUNCTIONS),
431         data, reply, option);
432     if (ret != UEC_OK) {
433         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
434         return ret;
435     }
436     READ_PARCEL_WITH_RET(reply, Int32, funcs, UEC_INTERFACE_READ_PARCEL_ERROR);
437     return ret;
438 }
439 
SetCurrentFunctions(int32_t funcs)440 int32_t UsbServerProxy::SetCurrentFunctions(int32_t funcs)
441 {
442     sptr<IRemoteObject> remote = Remote();
443     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
444 
445     MessageOption option;
446     MessageParcel data;
447     MessageParcel reply;
448 
449     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
450         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
451         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
452     }
453     WRITE_PARCEL_WITH_RET(data, Int32, funcs, UEC_INTERFACE_WRITE_PARCEL_ERROR);
454     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_CURRENT_FUNCTIONS),
455         data, reply, option);
456     if (ret != UEC_OK) {
457         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
458     }
459     return ret;
460 }
461 
UsbFunctionsFromString(std::string_view funcs)462 int32_t UsbServerProxy::UsbFunctionsFromString(std::string_view funcs)
463 {
464     sptr<IRemoteObject> remote = Remote();
465     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
466     MessageOption option;
467     MessageParcel data;
468     MessageParcel reply;
469 
470     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
471         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
472         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
473     }
474     WRITE_PARCEL_WITH_RET(data, String, std::string {funcs}, UEC_INTERFACE_WRITE_PARCEL_ERROR);
475     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_FROM_STRING),
476         data, reply, option);
477     if (ret != UEC_OK) {
478         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
479         return UEC_INTERFACE_INVALID_VALUE;
480     }
481     int32_t result = 0;
482     READ_PARCEL_WITH_RET(reply, Int32, result, INVALID_USB_INT_VALUE);
483     return result;
484 }
485 
UsbFunctionsToString(int32_t funcs)486 std::string UsbServerProxy::UsbFunctionsToString(int32_t funcs)
487 {
488     sptr<IRemoteObject> remote = Remote();
489 
490     MessageParcel data;
491     MessageOption option;
492     MessageParcel reply;
493 
494     RETURN_IF_WITH_RET(remote == nullptr, INVALID_STRING_VALUE);
495 
496     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
497         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
498         return INVALID_STRING_VALUE;
499     }
500     WRITE_PARCEL_WITH_RET(data, Int32, funcs, INVALID_STRING_VALUE);
501     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_TO_STRING),
502         data, reply, option);
503     if (ret != UEC_OK) {
504         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
505         return INVALID_STRING_VALUE;
506     }
507     std::string result;
508     READ_PARCEL_WITH_RET(reply, String, result, INVALID_STRING_VALUE);
509     return result;
510 }
511 
GetPorts(std::vector<UsbPort> &ports)512 int32_t UsbServerProxy::GetPorts(std::vector<UsbPort> &ports)
513 {
514     MessageOption option;
515     sptr<IRemoteObject> remote = Remote();
516 
517     MessageParcel data;
518     MessageParcel reply;
519     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
520     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
521         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
522         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
523     }
524     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_PORTS), data, reply, option);
525     if (ret != UEC_OK) {
526         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
527         return ret;
528     }
529     int32_t size;
530     READ_PARCEL_WITH_RET(reply, Int32, size, UEC_INTERFACE_READ_PARCEL_ERROR);
531     USB_HILOGI(MODULE_USB_INNERKIT, "GetPorts size %{public}d", size);
532     if (size > MAX_PORT_NUM) {
533         USB_HILOGE(MODULE_INNERKIT, "the maximum number of ports is exceeded!");
534         return ERR_INVALID_VALUE;
535     }
536     for (int32_t i = 0; i < size; ++i) {
537         USB_HILOGI(MODULE_USB_INNERKIT, "ParseUsbPort : %{public}d", i);
538         ret = ParseUsbPort(reply, ports);
539         if (ret) {
540             return ret;
541         }
542     }
543     return ret;
544 }
545 
ParseUsbPort(MessageParcel &reply, std::vector<UsbPort> &ports)546 int32_t UsbServerProxy::ParseUsbPort(MessageParcel &reply, std::vector<UsbPort> &ports)
547 {
548     UsbPort port;
549     UsbPortStatus status;
550     READ_PARCEL_WITH_RET(reply, Int32, port.id, UEC_INTERFACE_READ_PARCEL_ERROR);
551     USB_HILOGI(MODULE_USB_INNERKIT, "UsbServerProxy::port->id %{public}d", port.id);
552     port.supportedModes = reply.ReadInt32();
553     status.currentMode = reply.ReadInt32();
554     status.currentPowerRole = reply.ReadInt32();
555     status.currentDataRole = reply.ReadInt32();
556     port.usbPortStatus = status;
557     USB_HILOGI(MODULE_USB_INNERKIT, "UsbServerProxy::port.usbPortStatus.currentMode %{public}d",
558         port.usbPortStatus.currentMode);
559     ports.push_back(port);
560     return UEC_OK;
561 }
562 
GetSupportedModes(int32_t portId, int32_t &supportedModes)563 int32_t UsbServerProxy::GetSupportedModes(int32_t portId, int32_t &supportedModes)
564 {
565     MessageParcel data;
566     MessageParcel reply;
567     MessageOption option;
568     sptr<IRemoteObject> remote = Remote();
569     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
570     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
571         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
572         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
573     }
574     WRITE_PARCEL_WITH_RET(data, Int32, portId, UEC_INTERFACE_WRITE_PARCEL_ERROR);
575     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_SUPPORTED_MODES),
576         data, reply, option);
577     if (ret) {
578         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
579         return ret;
580     }
581     READ_PARCEL_WITH_RET(reply, Int32, supportedModes, UEC_INTERFACE_READ_PARCEL_ERROR);
582     return ret;
583 }
584 
SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)585 int32_t UsbServerProxy::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
586 {
587     MessageParcel data;
588     MessageParcel reply;
589     MessageOption option;
590     sptr<IRemoteObject> remote = Remote();
591     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
592     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
593         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
594         return UEC_INTERFACE_WRITE_PARCEL_ERROR;
595     }
596     WRITE_PARCEL_WITH_RET(data, Int32, portId, UEC_INTERFACE_WRITE_PARCEL_ERROR);
597     WRITE_PARCEL_WITH_RET(data, Int32, powerRole, UEC_INTERFACE_WRITE_PARCEL_ERROR);
598     WRITE_PARCEL_WITH_RET(data, Int32, dataRole, UEC_INTERFACE_WRITE_PARCEL_ERROR);
599     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_PORT_ROLE),
600         data, reply, option);
601     if (ret) {
602         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
603         return ret;
604     }
605     return ret;
606 }
607 
ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t force)608 int32_t UsbServerProxy::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface, uint8_t force)
609 {
610     sptr<IRemoteObject> remote = Remote();
611     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
612     MessageParcel data;
613     MessageParcel reply;
614     MessageOption option;
615     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
616         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
617         return ERR_ENOUGH_DATA;
618     }
619     SetDeviceMessage(data, busNum, devAddr);
620     WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
621     WRITE_PARCEL_WITH_RET(data, Uint8, force, UEC_SERVICE_WRITE_PARCEL_ERROR);
622     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_CLAIM_INTERFACE),
623         data, reply, option);
624     if (ret != UEC_OK) {
625         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
626         return ret;
627     }
628     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
629     return ret;
630 }
631 
UsbAttachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interface)632 int32_t UsbServerProxy::UsbAttachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interface)
633 {
634     sptr<IRemoteObject> remote = Remote();
635     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
636     MessageParcel data;
637     MessageParcel reply;
638     MessageOption option;
639     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
640         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
641         return ERR_ENOUGH_DATA;
642     }
643     SetDeviceMessage(data, busNum, devAddr);
644     WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
645     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_ATTACH_KERNEL_DRIVER),
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     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
652     return ret;
653 }
654 
UsbDetachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interface)655 int32_t UsbServerProxy::UsbDetachKernelDriver(uint8_t busNum, uint8_t devAddr, uint8_t interface)
656 {
657     sptr<IRemoteObject> remote = Remote();
658     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
659     MessageParcel data;
660     MessageParcel reply;
661     MessageOption option;
662     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
663         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
664         return ERR_ENOUGH_DATA;
665     }
666     SetDeviceMessage(data, busNum, devAddr);
667     WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
668     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DETACH_KERNEL_DRIVER),
669         data, reply, option);
670     if (ret != UEC_OK) {
671         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
672         return ret;
673     }
674     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
675     return ret;
676 }
677 
ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface)678 int32_t UsbServerProxy::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface)
679 {
680     sptr<IRemoteObject> remote = Remote();
681     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
682     MessageParcel data;
683     MessageParcel reply;
684     MessageOption option;
685     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
686         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
687         return ERR_ENOUGH_DATA;
688     }
689     SetDeviceMessage(data, busNum, devAddr);
690     WRITE_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
691     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_RELEASE_INTERFACE),
692         data, reply, option);
693     if (ret != UEC_OK) {
694         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
695         return ret;
696     }
697     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
698     return ret;
699 }
BulkTransferRead( const UsbDev &dev, const UsbPipe &pipe, std::vector<uint8_t> &bufferData, int32_t timeOut)700 int32_t UsbServerProxy::BulkTransferRead(
701     const UsbDev &dev, const UsbPipe &pipe, std::vector<uint8_t> &bufferData, int32_t timeOut)
702 {
703     sptr<IRemoteObject> remote = Remote();
704     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
705     MessageParcel data;
706     MessageParcel reply;
707     MessageOption option;
708     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
709         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
710         return ERR_ENOUGH_DATA;
711     }
712     SetDeviceMessage(data, dev.busNum, dev.devAddr);
713     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
714     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, 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),
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 
BulkTransferReadwithLength(const UsbDev &dev, const UsbPipe &pipe, int32_t length, std::vector<uint8_t> &bufferData, int32_t timeOut)732 int32_t UsbServerProxy::BulkTransferReadwithLength(const UsbDev &dev, const UsbPipe &pipe,
733     int32_t length, 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, length, UEC_SERVICE_WRITE_PARCEL_ERROR);
748     WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
749     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ_WITH_LENGTH),
750         data, reply, option);
751     if (ret != UEC_OK) {
752         USB_HILOGE(MODULE_USB_INNERKIT, "SendRequest is failed, error code: %{public}d", ret);
753         return ret;
754     }
755     ret = GetBufferMessage(reply, bufferData);
756     if (ret != UEC_OK) {
757         USB_HILOGE(MODULE_USB_INNERKIT, "get buffer is failed, error code: %{public}d", ret);
758         return ret;
759     }
760     USB_HILOGI(MODULE_USBD, "Set buffer message. length = %{public}zu", bufferData.size());
761     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
762     return ret;
763 }
764 
BulkTransferWrite( const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData, int32_t timeOut)765 int32_t UsbServerProxy::BulkTransferWrite(
766     const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData, int32_t timeOut)
767 {
768     sptr<IRemoteObject> remote = Remote();
769     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
770     MessageParcel data;
771     MessageParcel reply;
772     MessageOption option;
773     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
774         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
775         return ERR_ENOUGH_DATA;
776     }
777     SetDeviceMessage(data, dev.busNum, dev.devAddr);
778     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
779     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
780     WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
781     int32_t ret = SetBufferMessage(data, bufferData);
782     if (UEC_OK != ret) {
783         USB_HILOGE(MODULE_INNERKIT, "SetBufferMessage ret:%{public}d", ret);
784         return ret;
785     }
786     ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_WRITE),
787         data, reply, option);
788     if (UEC_OK != ret) {
789         USB_HILOGE(MODULE_INNERKIT, "SendRequest ret:%{public}d", ret);
790         return ret;
791     }
792     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
793     return ret;
794 }
795 
ControlTransfer( const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)796 int32_t UsbServerProxy::ControlTransfer(
797     const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
798 {
799     sptr<IRemoteObject> remote = Remote();
800     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
801     MessageParcel data;
802     MessageParcel reply;
803     MessageOption option;
804     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
805         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
806         return UEC_SERVICE_INNER_ERR;
807     }
808     SetDeviceMessage(data, dev.busNum, dev.devAddr);
809     WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
810     WRITE_PARCEL_WITH_RET(data, Int32, ctrl.requestCmd, UEC_SERVICE_WRITE_PARCEL_ERROR);
811     WRITE_PARCEL_WITH_RET(data, Int32, ctrl.value, UEC_SERVICE_WRITE_PARCEL_ERROR);
812     WRITE_PARCEL_WITH_RET(data, Int32, ctrl.index, UEC_SERVICE_WRITE_PARCEL_ERROR);
813     WRITE_PARCEL_WITH_RET(data, Int32, ctrl.timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
814     int32_t ret = SetBufferMessage(data, bufferData);
815     if (UEC_OK != ret) {
816         USB_HILOGE(MODULE_INNERKIT, "write failed! len:%{public}d", ret);
817         return ret;
818     }
819 
820     uint32_t reqType = static_cast<uint32_t>(ctrl.requestType);
821     bool isWrite = ((reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
822     ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_CONTROL_TRANSFER), data, reply, option);
823     if (ret != UEC_OK) {
824         USB_HILOGE(MODULE_INNERKIT, "USB_FUN_CONTROL_TRANSFER ret:%{public}d", ret);
825         return ret;
826     }
827     if (!isWrite) {
828         ret = GetBufferMessage(reply, bufferData);
829         if (UEC_OK != ret) {
830             USB_HILOGE(MODULE_USBD, "Get buffer message error. ret = %{public}d", ret);
831             return ret;
832         }
833         USB_HILOGI(MODULE_USBD, "Get buffer message. length = %{public}zu", bufferData.size());
834     }
835     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
836     return ret;
837 }
838 
UsbControlTransfer( const UsbDev &dev, const UsbCtrlTransferParams &ctrlParams, std::vector<uint8_t> &bufferData)839 int32_t UsbServerProxy::UsbControlTransfer(
840     const UsbDev &dev, const UsbCtrlTransferParams &ctrlParams, std::vector<uint8_t> &bufferData)
841 {
842     sptr<IRemoteObject> remote = Remote();
843     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
844     MessageParcel data;
845     MessageParcel reply;
846     MessageOption option;
847     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
848         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
849         return UEC_SERVICE_INNER_ERR;
850     }
851     SetDeviceMessage(data, dev.busNum, dev.devAddr);
852     WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
853     WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.requestCmd, UEC_SERVICE_WRITE_PARCEL_ERROR);
854     WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.value, UEC_SERVICE_WRITE_PARCEL_ERROR);
855     WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.index, UEC_SERVICE_WRITE_PARCEL_ERROR);
856     WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.length, UEC_SERVICE_WRITE_PARCEL_ERROR);
857     WRITE_PARCEL_WITH_RET(data, Int32, ctrlParams.timeout, UEC_SERVICE_WRITE_PARCEL_ERROR);
858     int32_t ret = SetBufferMessage(data, bufferData);
859     if (UEC_OK != ret) {
860         USB_HILOGE(MODULE_INNERKIT, "write failed! len:%{public}d", ret);
861         return ret;
862     }
863 
864     uint32_t reqType = static_cast<uint32_t>(ctrlParams.requestType);
865     bool isWrite = ((reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
866     ret = remote->SendRequest(
867         static_cast<int32_t>(UsbInterfaceCode::USB_FUN_USB_CONTROL_TRANSFER), data, reply, option);
868     if (ret != UEC_OK) {
869         USB_HILOGE(MODULE_INNERKIT, "USB_FUN_USB_CONTROL_TRANSFER ret:%{public}d", ret);
870         return ret;
871     }
872     if (!isWrite) {
873         ret = GetBufferMessage(reply, bufferData);
874         if (UEC_OK != ret) {
875             USB_HILOGE(MODULE_USBD, "Get buffer message error. ret = %{public}d", ret);
876             return ret;
877         }
878         USB_HILOGI(MODULE_USBD, "Get buffer message. length = %{public}zu", bufferData.size());
879     }
880     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
881     return ret;
882 }
883 
SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex)884 int32_t UsbServerProxy::SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex)
885 {
886     sptr<IRemoteObject> remote = Remote();
887     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
888     MessageParcel data;
889     MessageParcel reply;
890     MessageOption option;
891     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
892         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
893         return ERR_ENOUGH_DATA;
894     }
895     SetDeviceMessage(data, busNum, devAddr);
896     WRITE_PARCEL_WITH_RET(data, Uint8, configIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
897     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_ACTIVE_CONFIG),
898         data, reply, option);
899     if (UEC_OK != ret) {
900         USB_HILOGE(MODULE_INNERKIT, "USB_FUN_SET_ACTIVE_CONFIG ret:%{public}d", ret);
901         return ret;
902     }
903     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
904     return ret;
905 }
GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex)906 int32_t UsbServerProxy::GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex)
907 {
908     sptr<IRemoteObject> remote = Remote();
909     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
910     MessageParcel data;
911     MessageParcel reply;
912     MessageOption option;
913     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
914         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
915         return ERR_ENOUGH_DATA;
916     }
917     SetDeviceMessage(data, busNum, devAddr);
918     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_ACTIVE_CONFIG),
919         data, reply, option);
920     if (ret != UEC_OK) {
921         USB_HILOGE(MODULE_INNERKIT, "USB_FUN_GET_ACTIVE_CONFIG ret:%{public}d", ret);
922         return ret;
923     }
924     READ_PARCEL_WITH_RET(reply, Uint8, configIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
925     return ret;
926 }
SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex)927 int32_t UsbServerProxy::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex)
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, "write descriptor failed!");
936         return ERR_ENOUGH_DATA;
937     }
938     SetDeviceMessage(data, busNum, devAddr);
939     WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
940     WRITE_PARCEL_WITH_RET(data, Uint8, altIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
941     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_SET_INTERFACE),
942         data, reply, option);
943     if (UEC_OK != ret) {
944         USB_HILOGE(MODULE_INNERKIT, "USB_FUN_SET_INTERFACE ret:%{public}d", ret);
945         return ret;
946     }
947     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
948     return ret;
949 }
GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData)950 int32_t UsbServerProxy::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData)
951 {
952     sptr<IRemoteObject> remote = Remote();
953     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
954     MessageParcel data;
955     MessageParcel reply;
956     MessageOption option;
957     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
958         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
959         return ERR_ENOUGH_DATA;
960     }
961     SetDeviceMessage(data, busNum, devAddr);
962     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_DESCRIPTOR),
963         data, reply, option);
964     if (ret == UEC_OK) {
965         ret = GetBufferMessage(reply, bufferData);
966         if (UEC_OK != ret) {
967             USB_HILOGE(MODULE_INNERKIT, "get failed ret:%{public}d", ret);
968         }
969     }
970     return ret;
971 }
972 
GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd)973 int32_t UsbServerProxy::GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd)
974 {
975     sptr<IRemoteObject> remote = Remote();
976     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
977     MessageParcel data;
978     MessageParcel reply;
979     MessageOption option;
980     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
981         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
982         return ERR_ENOUGH_DATA;
983     }
984     SetDeviceMessage(data, busNum, devAddr);
985     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_FILEDESCRIPTOR),
986         data, reply, option);
987     if (ret == UEC_OK) {
988         fd = -1;
989         if (!ReadFileDescriptor(reply, fd)) {
990             USB_HILOGW(MODULE_USB_SERVICE, "%{public}s: read fd failed!", __func__);
991             return UEC_INTERFACE_READ_PARCEL_ERROR;
992         }
993     }
994     return ret;
995 }
996 
RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData, const std::vector<uint8_t> &bufferData)997 int32_t UsbServerProxy::RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
998     const std::vector<uint8_t> &bufferData)
999 {
1000     sptr<IRemoteObject> remote = Remote();
1001     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1002     MessageParcel data;
1003     MessageParcel reply;
1004     MessageOption option;
1005     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1006         USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
1007         return ERR_ENOUGH_DATA;
1008     }
1009     SetDeviceMessage(data, dev.busNum, dev.devAddr);
1010     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1011     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1012 
1013     int32_t ret = UsbServerProxy::SetBufferMessage(data, clientData);
1014     if (UEC_OK != ret) {
1015         USB_HILOGE(MODULE_INNERKIT, "set clientData failed ret:%{public}d", ret);
1016         return ERR_INVALID_VALUE;
1017     }
1018 
1019     ret = UsbServerProxy::SetBufferMessage(data, bufferData);
1020     if (UEC_OK != ret) {
1021         USB_HILOGE(MODULE_INNERKIT, "setBuffer failed ret:%{public}d", ret);
1022         return ERR_INVALID_VALUE;
1023     }
1024 
1025     ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_QUEUE), data, reply, option);
1026     if (ret != UEC_OK) {
1027         USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1028         return ret;
1029     }
1030     return ret;
1031 }
1032 
RequestWait( const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData, std::vector<uint8_t> &bufferData)1033 int32_t UsbServerProxy::RequestWait(
1034     const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData, std::vector<uint8_t> &bufferData)
1035 {
1036     sptr<IRemoteObject> remote = Remote();
1037     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1038     MessageParcel data;
1039     MessageParcel reply;
1040     MessageOption option;
1041 
1042     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1043         USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
1044         return ERR_ENOUGH_DATA;
1045     }
1046 
1047     SetDeviceMessage(data, dev.busNum, dev.devAddr);
1048     WRITE_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
1049     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_WAIT),
1050         data, reply, option);
1051     if (ret != UEC_OK) {
1052         USB_HILOGE(MODULE_INNERKIT, "queue failed! ret:%{public}d", ret);
1053         return ret;
1054     }
1055 
1056     ret = UsbServerProxy::GetBufferMessage(reply, clientData);
1057     if (ret != UEC_OK) {
1058         USB_HILOGE(MODULE_INNERKIT, "get clientData failed! ret:%{public}d", ret);
1059         return ret;
1060     }
1061 
1062     ret = UsbServerProxy::GetBufferMessage(reply, bufferData);
1063     if (ret != UEC_OK) {
1064         USB_HILOGE(MODULE_INNERKIT, "get buffer failed! ret:%{public}d", ret);
1065         return ret;
1066     }
1067 
1068     return ret;
1069 }
1070 
RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId)1071 int32_t UsbServerProxy::RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId)
1072 {
1073     int32_t ret;
1074     sptr<IRemoteObject> remote = Remote();
1075     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1076     MessageParcel data;
1077     MessageParcel reply;
1078     MessageOption option;
1079     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1080         USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
1081         return ERR_ENOUGH_DATA;
1082     }
1083 
1084     SetDeviceMessage(data, busNum, devAddr);
1085     WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
1086     WRITE_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1087     ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REQUEST_CANCEL), data, reply, option);
1088     if (ret != UEC_OK) {
1089         USB_HILOGE(MODULE_INNERKIT, "request cancel failed!");
1090     }
1091 
1092     return ret;
1093 }
1094 
Close(uint8_t busNum, uint8_t devAddr)1095 int32_t UsbServerProxy::Close(uint8_t busNum, uint8_t devAddr)
1096 {
1097     sptr<IRemoteObject> remote = Remote();
1098     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1099     MessageOption option;
1100     MessageParcel data;
1101     MessageParcel reply;
1102     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1103         USB_HILOGE(MODULE_INNERKIT, "get descriptor failed!");
1104         return ERR_ENOUGH_DATA;
1105     }
1106 
1107     SetDeviceMessage(data, busNum, devAddr);
1108     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_CLOSE_DEVICE),
1109         data, reply, option);
1110     if (ret != UEC_OK) {
1111         USB_HILOGE(MODULE_INNERKIT, "queue failed!");
1112         return ret;
1113     }
1114     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
1115     return ret;
1116 }
1117 
RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IRemoteObject> &cb)1118 int32_t UsbServerProxy::RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IRemoteObject> &cb)
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, RemoteObject, cb, UEC_SERVICE_WRITE_PARCEL_ERROR);
1131     MessageOption option;
1132     MessageParcel reply;
1133     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_REG_BULK_CALLBACK),
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 
UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe)1142 int32_t UsbServerProxy::UnRegBulkCallback(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_UNREG_BULK_CALLBACK),
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 
BulkRead(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)1165 int32_t UsbServerProxy::BulkRead(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
1166 {
1167     sptr<IRemoteObject> remote = Remote();
1168     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1169     MessageParcel data;
1170     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1171         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1172         return ERR_ENOUGH_DATA;
1173     }
1174     SetDeviceMessage(data, dev.busNum, dev.devAddr);
1175     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1176     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1177     WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
1178     MessageOption option;
1179     MessageParcel reply;
1180     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_READ),
1181         data, reply, option);
1182     if (ret != UEC_OK) {
1183         USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1184         return ret;
1185     }
1186     return ret;
1187 }
1188 
BulkWrite(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)1189 int32_t UsbServerProxy::BulkWrite(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem)
1190 {
1191     sptr<IRemoteObject> remote = Remote();
1192     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1193     MessageParcel data;
1194     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1195         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1196         return ERR_ENOUGH_DATA;
1197     }
1198     SetDeviceMessage(data, dev.busNum, dev.devAddr);
1199     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1200     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1201     WRITE_PARCEL_WITH_RET(data, Ashmem, ashmem, UEC_SERVICE_WRITE_PARCEL_ERROR);
1202     MessageOption option;
1203     MessageParcel reply;
1204     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_WRITE),
1205         data, reply, option);
1206     if (ret != UEC_OK) {
1207         USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1208         return ret;
1209     }
1210     return ret;
1211 }
1212 
BulkCancel(const UsbDev &dev, const UsbPipe &pipe)1213 int32_t UsbServerProxy::BulkCancel(const UsbDev &dev, const UsbPipe &pipe)
1214 {
1215     sptr<IRemoteObject> remote = Remote();
1216     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1217     MessageParcel data;
1218     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1219         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1220         return ERR_ENOUGH_DATA;
1221     }
1222     SetDeviceMessage(data, dev.busNum, dev.devAddr);
1223     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.intfId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1224     WRITE_PARCEL_WITH_RET(data, Uint8, pipe.endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1225     MessageOption option;
1226     MessageParcel reply;
1227     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_CANCEL),
1228         data, reply, option);
1229     if (ret != UEC_OK) {
1230         USB_HILOGE(MODULE_INNERKIT, "SendRequest failed!");
1231         return ret;
1232     }
1233     return ret;
1234 }
1235 
AddRight(const std::string &bundleName, const std::string &deviceName)1236 int32_t UsbServerProxy::AddRight(const std::string &bundleName, const std::string &deviceName)
1237 {
1238     sptr<IRemoteObject> remote = Remote();
1239     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1240 
1241     MessageParcel data;
1242     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1243         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1244         return ERR_ENOUGH_DATA;
1245     }
1246     WRITE_PARCEL_WITH_RET(data, String, bundleName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1247     WRITE_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1248 
1249     MessageOption option;
1250     MessageParcel reply;
1251     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_ADD_RIGHT), data, reply, option);
1252     if (ret != UEC_OK) {
1253         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1254     }
1255     return ret;
1256 }
1257 
AddAccessRight(const std::string &tokenId, const std::string &deviceName)1258 int32_t UsbServerProxy::AddAccessRight(const std::string &tokenId, const std::string &deviceName)
1259 {
1260     sptr<IRemoteObject> remote = Remote();
1261     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1262 
1263     MessageParcel data;
1264     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1265         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1266         return ERR_ENOUGH_DATA;
1267     }
1268     WRITE_PARCEL_WITH_RET(data, String, tokenId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1269     WRITE_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_WRITE_PARCEL_ERROR);
1270 
1271     MessageOption option;
1272     MessageParcel reply;
1273     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_ADD_ACCESS_RIGHT),
1274         data, reply, option);
1275     if (ret != UEC_OK) {
1276         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1277     }
1278     return ret;
1279 }
1280 
ManageGlobalInterface(bool disable)1281 int32_t UsbServerProxy::ManageGlobalInterface(bool disable)
1282 {
1283     sptr<IRemoteObject> remote = Remote();
1284     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1285 
1286     MessageParcel data;
1287     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1288         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1289         return ERR_ENOUGH_DATA;
1290     }
1291     WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR);
1292 
1293     MessageOption option;
1294     MessageParcel reply;
1295     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DISABLE_GLOBAL_INTERFACE),
1296         data, reply, option);
1297     if (ret != UEC_OK) {
1298         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1299     }
1300     return ret;
1301 }
1302 
ManageDevice(int32_t vendorId, int32_t productId, bool disable)1303 int32_t UsbServerProxy::ManageDevice(int32_t vendorId, int32_t productId, bool disable)
1304 {
1305     sptr<IRemoteObject> remote = Remote();
1306     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1307 
1308     MessageParcel data;
1309     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1310         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1311         return ERR_ENOUGH_DATA;
1312     }
1313     WRITE_PARCEL_WITH_RET(data, Int32, vendorId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1314     WRITE_PARCEL_WITH_RET(data, Int32, productId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1315     WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR);
1316 
1317     MessageOption option;
1318     MessageParcel reply;
1319     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DISABLE_DEVICE),
1320         data, reply, option);
1321     if (ret != UEC_OK) {
1322         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1323     }
1324     return ret;
1325 }
1326 
ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable)1327 int32_t UsbServerProxy::ManageInterfaceType(const std::vector<UsbDeviceType> &disableType, bool disable)
1328 {
1329     sptr<IRemoteObject> remote = Remote();
1330     RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
1331 
1332     MessageParcel data;
1333     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1334         USB_HILOGE(MODULE_USB_SERVICE, "write descriptor failed!");
1335         return ERR_ENOUGH_DATA;
1336     }
1337     int32_t size = (int32_t)disableType.size();
1338     WRITE_PARCEL_WITH_RET(data, Int32, size, UEC_SERVICE_WRITE_PARCEL_ERROR);
1339 
1340     for (const auto &type : disableType) {
1341         WRITE_PARCEL_WITH_RET(data, Int32, type.baseClass, UEC_SERVICE_WRITE_PARCEL_ERROR);
1342         WRITE_PARCEL_WITH_RET(data, Int32, type.subClass, UEC_SERVICE_WRITE_PARCEL_ERROR);
1343         WRITE_PARCEL_WITH_RET(data, Int32, type.protocol, UEC_SERVICE_WRITE_PARCEL_ERROR);
1344         WRITE_PARCEL_WITH_RET(data, Bool, type.isDeviceType, UEC_SERVICE_WRITE_PARCEL_ERROR);
1345     }
1346     WRITE_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_WRITE_PARCEL_ERROR);
1347 
1348     MessageOption option;
1349     MessageParcel reply;
1350     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_TYPE),
1351         data, reply, option);
1352     if (ret != UEC_OK) {
1353         USB_HILOGE(MODULE_USB_SERVICE, "SendRequest is failed, error code: %{public}d", ret);
1354     }
1355     return ret;
1356 }
1357 
ClearHalt(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointId)1358 int32_t UsbServerProxy::ClearHalt(uint8_t busNum, uint8_t devAddr, uint8_t interfaceId, uint8_t endpointId)
1359 {
1360     sptr<IRemoteObject> remote = Remote();
1361     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1362     MessageParcel data;
1363     MessageParcel reply;
1364     MessageOption option;
1365     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1366         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1367         return ERR_ENOUGH_DATA;
1368     }
1369     SetDeviceMessage(data, busNum, devAddr);
1370     WRITE_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1371     WRITE_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
1372     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_CLEAR_HALT), data, reply, option);
1373     if (ret != UEC_OK) {
1374         USB_HILOGE(MODULE_USB_SERVICE, "ClearHalt is failed, error code: %{public}d", ret);
1375         return ret;
1376     }
1377     READ_PARCEL_WITH_RET(reply, Int32, ret, UEC_INTERFACE_READ_PARCEL_ERROR);
1378     return ret;
1379 }
1380 
GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed)1381 int32_t UsbServerProxy::GetDeviceSpeed(uint8_t busNum, uint8_t devAddr, uint8_t &speed)
1382 {
1383     sptr<IRemoteObject> remote = Remote();
1384     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1385     MessageParcel data;
1386     MessageParcel reply;
1387     MessageOption option;
1388     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1389         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1390         return ERR_ENOUGH_DATA;
1391     }
1392     SetDeviceMessage(data, busNum, devAddr);
1393     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_DEVICE_SPEED),
1394         data, reply, option);
1395     if (ret == UEC_OK) {
1396         READ_PARCEL_WITH_RET(reply, Uint8, speed, UEC_INTERFACE_READ_PARCEL_ERROR);
1397     }
1398     USB_HILOGE(MODULE_INNERKIT, "GetDeviceSpeed speed:%{public}u", speed);
1399     return ret;
1400 }
1401 
GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, bool &unactivated)1402 int32_t UsbServerProxy::GetInterfaceActiveStatus(uint8_t busNum, uint8_t devAddr,
1403     uint8_t interfaceid,  bool &unactivated)
1404 {
1405     sptr<IRemoteObject> remote = Remote();
1406     RETURN_IF_WITH_RET(remote == nullptr, UEC_SERVICE_INNER_ERR);
1407     MessageParcel data;
1408     MessageParcel reply;
1409     MessageOption option;
1410     if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) {
1411         USB_HILOGE(MODULE_INNERKIT, "write descriptor failed!");
1412         return ERR_ENOUGH_DATA;
1413     }
1414     SetDeviceMessage(data, busNum, devAddr);
1415     WRITE_PARCEL_WITH_RET(data, Uint8, interfaceid, UEC_SERVICE_WRITE_PARCEL_ERROR);
1416     int32_t ret = remote->SendRequest(static_cast<int32_t>(UsbInterfaceCode::USB_FUN_GET_DRIVER_ACTIVE_STATUS),
1417         data, reply, option);
1418     if (ret == UEC_OK) {
1419         READ_PARCEL_WITH_RET(reply, Bool, unactivated, UEC_INTERFACE_READ_PARCEL_ERROR);
1420     }
1421     return ret;
1422 }
ReadFileDescriptor(MessageParcel &data, int &fd)1423 bool UsbServerProxy::ReadFileDescriptor(MessageParcel &data, int &fd)
1424 {
1425     fd = -1;
1426     bool fdValid = false;
1427     if (!data.ReadBool(fdValid)) {
1428         USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to read fdValid", __func__);
1429         return false;
1430     }
1431 
1432     if (fdValid) {
1433         fd = data.ReadFileDescriptor();
1434         if (fd < 0) {
1435             USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to read fd", __func__);
1436             return false;
1437         }
1438     }
1439     return true;
1440 }
1441 } // namespace USB
1442 } // namespace OHOS
1443