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