142103316Sopenharmony_ci/*
242103316Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
342103316Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
442103316Sopenharmony_ci * you may not use this file except in compliance with the License.
542103316Sopenharmony_ci * You may obtain a copy of the License at
642103316Sopenharmony_ci *
742103316Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
842103316Sopenharmony_ci *
942103316Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1042103316Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1142103316Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1242103316Sopenharmony_ci * See the License for the specific language governing permissions and
1342103316Sopenharmony_ci * limitations under the License.
1442103316Sopenharmony_ci */
1542103316Sopenharmony_ci
1642103316Sopenharmony_ci#include "message_parcel.h"
1742103316Sopenharmony_ci#include "securec.h"
1842103316Sopenharmony_ci#include "string_ex.h"
1942103316Sopenharmony_ci#include "usb_common.h"
2042103316Sopenharmony_ci#include "usb_errors.h"
2142103316Sopenharmony_ci#include "usb_server_stub.h"
2242103316Sopenharmony_ci#include "usb_interface_type.h"
2342103316Sopenharmony_ci#include "v1_1/iusb_interface.h"
2442103316Sopenharmony_ci#include "usb_report_sys_event.h"
2542103316Sopenharmony_ci#include "hitrace_meter.h"
2642103316Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_1;
2742103316Sopenharmony_cinamespace OHOS {
2842103316Sopenharmony_cinamespace USB {
2942103316Sopenharmony_ciconstexpr int32_t MAX_EDM_LIST_SIZE = 200;
3042103316Sopenharmony_ciint32_t UsbServerStub::GetDeviceMessage(MessageParcel &data, uint8_t &busNum, uint8_t &devAddr)
3142103316Sopenharmony_ci{
3242103316Sopenharmony_ci    if (!data.ReadUint8(busNum)) {
3342103316Sopenharmony_ci        return UEC_SERVICE_READ_PARCEL_ERROR;
3442103316Sopenharmony_ci    }
3542103316Sopenharmony_ci    if (!data.ReadUint8(devAddr)) {
3642103316Sopenharmony_ci        return UEC_SERVICE_READ_PARCEL_ERROR;
3742103316Sopenharmony_ci    }
3842103316Sopenharmony_ci    return UEC_OK;
3942103316Sopenharmony_ci}
4042103316Sopenharmony_ci
4142103316Sopenharmony_ciint32_t UsbServerStub::SetBufferMessage(MessageParcel &data, const std::vector<uint8_t> &bufferData)
4242103316Sopenharmony_ci{
4342103316Sopenharmony_ci    uint32_t length = bufferData.size();
4442103316Sopenharmony_ci    const uint8_t *ptr = bufferData.data();
4542103316Sopenharmony_ci    if (!ptr) {
4642103316Sopenharmony_ci        length = 0;
4742103316Sopenharmony_ci    }
4842103316Sopenharmony_ci
4942103316Sopenharmony_ci    if (!data.WriteUint32(length)) {
5042103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "write length failed length:%{public}u", length);
5142103316Sopenharmony_ci        return UEC_SERVICE_WRITE_PARCEL_ERROR;
5242103316Sopenharmony_ci    }
5342103316Sopenharmony_ci    if ((ptr) && (length > 0) && !data.WriteBuffer(reinterpret_cast<const void *>(ptr), length)) {
5442103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "writer buffer failed length:%{public}u", length);
5542103316Sopenharmony_ci        return UEC_SERVICE_WRITE_PARCEL_ERROR;
5642103316Sopenharmony_ci    }
5742103316Sopenharmony_ci    return UEC_OK;
5842103316Sopenharmony_ci}
5942103316Sopenharmony_ci
6042103316Sopenharmony_ciint32_t UsbServerStub::GetBufferMessage(MessageParcel &data, std::vector<uint8_t> &bufferData)
6142103316Sopenharmony_ci{
6242103316Sopenharmony_ci    uint32_t dataSize = 0;
6342103316Sopenharmony_ci    bufferData.clear();
6442103316Sopenharmony_ci    if (!data.ReadUint32(dataSize)) {
6542103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "read dataSize failed");
6642103316Sopenharmony_ci        return UEC_SERVICE_READ_PARCEL_ERROR;
6742103316Sopenharmony_ci    }
6842103316Sopenharmony_ci    if (dataSize == 0) {
6942103316Sopenharmony_ci        USB_HILOGW(MODULE_USBD, "size:%{public}u", dataSize);
7042103316Sopenharmony_ci        return UEC_OK;
7142103316Sopenharmony_ci    }
7242103316Sopenharmony_ci
7342103316Sopenharmony_ci    const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
7442103316Sopenharmony_ci    if (readData == nullptr) {
7542103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "failed size:%{public}u", dataSize);
7642103316Sopenharmony_ci        return UEC_SERVICE_READ_PARCEL_ERROR;
7742103316Sopenharmony_ci    }
7842103316Sopenharmony_ci    std::vector<uint8_t> tdata(readData, readData + dataSize);
7942103316Sopenharmony_ci    bufferData.swap(tdata);
8042103316Sopenharmony_ci    return UEC_OK;
8142103316Sopenharmony_ci}
8242103316Sopenharmony_ci
8342103316Sopenharmony_cibool UsbServerStub::WriteFileDescriptor(MessageParcel &data, int fd)
8442103316Sopenharmony_ci{
8542103316Sopenharmony_ci    if (!data.WriteBool(fd >= 0 ? true : false)) {
8642103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to write fd vailed", __func__);
8742103316Sopenharmony_ci        return false;
8842103316Sopenharmony_ci    }
8942103316Sopenharmony_ci    if (fd < 0) {
9042103316Sopenharmony_ci        return true;
9142103316Sopenharmony_ci    }
9242103316Sopenharmony_ci    if (!data.WriteFileDescriptor(fd)) {
9342103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: failed to write fd", __func__);
9442103316Sopenharmony_ci        return false;
9542103316Sopenharmony_ci    }
9642103316Sopenharmony_ci    return true;
9742103316Sopenharmony_ci}
9842103316Sopenharmony_ci
9942103316Sopenharmony_cibool UsbServerStub::StubDevice(
10042103316Sopenharmony_ci    uint32_t code, int32_t &result, MessageParcel &data, MessageParcel &reply, MessageOption &option)
10142103316Sopenharmony_ci{
10242103316Sopenharmony_ci    switch (code) {
10342103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_OPEN_DEVICE):
10442103316Sopenharmony_ci            result = DoOpenDevice(data, reply, option);
10542103316Sopenharmony_ci            return true;
10642103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_RESET_DEVICE):
10742103316Sopenharmony_ci            result = DoResetDevice(data, reply, option);
10842103316Sopenharmony_ci            return true;
10942103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_HAS_RIGHT):
11042103316Sopenharmony_ci            result = DoHasRight(data, reply, option);
11142103316Sopenharmony_ci            return true;
11242103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_RIGHT):
11342103316Sopenharmony_ci            result = DoRequestRight(data, reply, option);
11442103316Sopenharmony_ci            return true;
11542103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_REMOVE_RIGHT):
11642103316Sopenharmony_ci            result = DoRemoveRight(data, reply, option);
11742103316Sopenharmony_ci            return true;
11842103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_PORTS):
11942103316Sopenharmony_ci            result = DoGetPorts(data, reply, option);
12042103316Sopenharmony_ci            return true;
12142103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_SUPPORTED_MODES):
12242103316Sopenharmony_ci            result = DoGetSupportedModes(data, reply, option);
12342103316Sopenharmony_ci            return true;
12442103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_PORT_ROLE):
12542103316Sopenharmony_ci            result = DoSetPortRole(data, reply, option);
12642103316Sopenharmony_ci            return true;
12742103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_CLAIM_INTERFACE):
12842103316Sopenharmony_ci            result = DoClaimInterface(data, reply, option);
12942103316Sopenharmony_ci            return true;
13042103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_ATTACH_KERNEL_DRIVER):
13142103316Sopenharmony_ci            result = DoUsbAttachKernelDriver(data, reply, option);
13242103316Sopenharmony_ci            return true;
13342103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_DETACH_KERNEL_DRIVER):
13442103316Sopenharmony_ci            result = DoUsbDetachKernelDriver(data, reply, option);
13542103316Sopenharmony_ci            return true;
13642103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_RELEASE_INTERFACE):
13742103316Sopenharmony_ci            result = DoReleaseInterface(data, reply, option);
13842103316Sopenharmony_ci            return true;
13942103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_REG_BULK_CALLBACK):
14042103316Sopenharmony_ci            result = DoRegBulkCallback(data, reply, option);
14142103316Sopenharmony_ci            return true;
14242103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_UNREG_BULK_CALLBACK):
14342103316Sopenharmony_ci            result = DoUnRegBulkCallback(data, reply, option);
14442103316Sopenharmony_ci            return true;
14542103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_FILEDESCRIPTOR):
14642103316Sopenharmony_ci            result = DoGetFileDescriptor(data, reply, option);
14742103316Sopenharmony_ci            return true;
14842103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_ADD_RIGHT):
14942103316Sopenharmony_ci            result = DoAddRight(data, reply, option);
15042103316Sopenharmony_ci            return true;
15142103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_ADD_ACCESS_RIGHT):
15242103316Sopenharmony_ci            result = DoAddAccessRight(data, reply, option);
15342103316Sopenharmony_ci            return true;
15442103316Sopenharmony_ci        default:;
15542103316Sopenharmony_ci    }
15642103316Sopenharmony_ci    return false;
15742103316Sopenharmony_ci}
15842103316Sopenharmony_ci
15942103316Sopenharmony_cibool UsbServerStub::StubHost(
16042103316Sopenharmony_ci    uint32_t code, int32_t &result, MessageParcel &data, MessageParcel &reply, MessageOption &option)
16142103316Sopenharmony_ci{
16242103316Sopenharmony_ci    switch (code) {
16342103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DEVICES):
16442103316Sopenharmony_ci            result = DoGetDevices(data, reply, option);
16542103316Sopenharmony_ci            return true;
16642103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_CURRENT_FUNCTIONS):
16742103316Sopenharmony_ci            result = DoGetCurrentFunctions(data, reply, option);
16842103316Sopenharmony_ci            return true;
16942103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_CURRENT_FUNCTIONS):
17042103316Sopenharmony_ci            result = DoSetCurrentFunctions(data, reply, option);
17142103316Sopenharmony_ci            return true;
17242103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_FROM_STRING):
17342103316Sopenharmony_ci            result = DoUsbFunctionsFromString(data, reply, option);
17442103316Sopenharmony_ci            return true;
17542103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_FUNCTIONS_TO_STRING):
17642103316Sopenharmony_ci            result = DoUsbFunctionsToString(data, reply, option);
17742103316Sopenharmony_ci            return true;
17842103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_CLOSE_DEVICE):
17942103316Sopenharmony_ci            result = DoClose(data, reply, option);
18042103316Sopenharmony_ci            return true;
18142103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_QUEUE):
18242103316Sopenharmony_ci            result = DoRequestQueue(data, reply, option);
18342103316Sopenharmony_ci            return true;
18442103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_WAIT):
18542103316Sopenharmony_ci            result = DoRequestWait(data, reply, option);
18642103316Sopenharmony_ci            return true;
18742103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_INTERFACE):
18842103316Sopenharmony_ci            result = DoSetInterface(data, reply, option);
18942103316Sopenharmony_ci            return true;
19042103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_SET_ACTIVE_CONFIG):
19142103316Sopenharmony_ci            result = DoSetActiveConfig(data, reply, option);
19242103316Sopenharmony_ci            return true;
19342103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_REQUEST_CANCEL):
19442103316Sopenharmony_ci            result = DoRequestCancel(data, reply, option);
19542103316Sopenharmony_ci            return true;
19642103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_READ):
19742103316Sopenharmony_ci            result = DoBulkRead(data, reply, option);
19842103316Sopenharmony_ci            return true;
19942103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_WRITE):
20042103316Sopenharmony_ci            result = DoBulkWrite(data, reply, option);
20142103316Sopenharmony_ci            return true;
20242103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_AYSNC_CANCEL):
20342103316Sopenharmony_ci            result = DoBulkCancel(data, reply, option);
20442103316Sopenharmony_ci            return true;
20542103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DESCRIPTOR):
20642103316Sopenharmony_ci            result = DoGetRawDescriptor(data, reply, option);
20742103316Sopenharmony_ci            return true;
20842103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_DISABLE_GLOBAL_INTERFACE):
20942103316Sopenharmony_ci            result = DoManageGlobalInterface(data, reply, option);
21042103316Sopenharmony_ci            return true;
21142103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_DISABLE_DEVICE):
21242103316Sopenharmony_ci            result = DoManageDevice(data, reply, option);
21342103316Sopenharmony_ci            return true;
21442103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_DISABLE_INTERFACE_TYPE):
21542103316Sopenharmony_ci            result = DoManageInterfaceType(data, reply, option);
21642103316Sopenharmony_ci            return true;
21742103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_CLEAR_HALT):
21842103316Sopenharmony_ci            result = DoClearHalt(data, reply, option);
21942103316Sopenharmony_ci            return true;
22042103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DEVICE_SPEED):
22142103316Sopenharmony_ci            result = DoGetDeviceSpeed(data, reply, option);
22242103316Sopenharmony_ci            return true;
22342103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_GET_DRIVER_ACTIVE_STATUS):
22442103316Sopenharmony_ci            result = DoGetInterfaceActiveStatus(data, reply, option);
22542103316Sopenharmony_ci            return true;
22642103316Sopenharmony_ci        default:;
22742103316Sopenharmony_ci    }
22842103316Sopenharmony_ci    return false;
22942103316Sopenharmony_ci}
23042103316Sopenharmony_ci
23142103316Sopenharmony_cibool UsbServerStub::StubHostTransfer(uint32_t code, int32_t &result,
23242103316Sopenharmony_ci    MessageParcel &data, MessageParcel &reply, MessageOption &option)
23342103316Sopenharmony_ci{
23442103316Sopenharmony_ci    switch (code) {
23542103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ):
23642103316Sopenharmony_ci            result = DoBulkTransferRead(data, reply, option);
23742103316Sopenharmony_ci            return true;
23842103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_READ_WITH_LENGTH):
23942103316Sopenharmony_ci            result = DoBulkTransferReadwithLength(data, reply, option);
24042103316Sopenharmony_ci            return true;
24142103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_BULK_TRANSFER_WRITE):
24242103316Sopenharmony_ci            result = DoBulkTransferWrite(data, reply, option);
24342103316Sopenharmony_ci            return true;
24442103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_CONTROL_TRANSFER):
24542103316Sopenharmony_ci            result = DoControlTransfer(data, reply, option);
24642103316Sopenharmony_ci            return true;
24742103316Sopenharmony_ci        case static_cast<int>(UsbInterfaceCode::USB_FUN_USB_CONTROL_TRANSFER):
24842103316Sopenharmony_ci            result = DoUsbControlTransfer(data, reply, option);
24942103316Sopenharmony_ci            return true;
25042103316Sopenharmony_ci        default:;
25142103316Sopenharmony_ci    }
25242103316Sopenharmony_ci    return false;
25342103316Sopenharmony_ci}
25442103316Sopenharmony_ci
25542103316Sopenharmony_ciint32_t UsbServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
25642103316Sopenharmony_ci{
25742103316Sopenharmony_ci    USB_HILOGD(MODULE_USB_SERVICE, "UsbServerStub::OnRemoteRequest, cmd = %{public}u, flags = %{public}d", code,
25842103316Sopenharmony_ci        option.GetFlags());
25942103316Sopenharmony_ci    std::u16string descriptor = UsbServerStub::GetDescriptor();
26042103316Sopenharmony_ci    std::u16string remoteDescriptor = data.ReadInterfaceToken();
26142103316Sopenharmony_ci    if (descriptor != remoteDescriptor) {
26242103316Sopenharmony_ci        USB_HILOGE(MODULE_SERVICE, "UsbServerStub::OnRemoteRequest failed, descriptor is not matched!");
26342103316Sopenharmony_ci        return UEC_SERVICE_INNER_ERR;
26442103316Sopenharmony_ci    }
26542103316Sopenharmony_ci
26642103316Sopenharmony_ci    int32_t ret = 0;
26742103316Sopenharmony_ci    if (StubHost(code, ret, data, reply, option)) {
26842103316Sopenharmony_ci        return ret;
26942103316Sopenharmony_ci    } else if (StubDevice(code, ret, data, reply, option)) {
27042103316Sopenharmony_ci        return ret;
27142103316Sopenharmony_ci    } else if (StubHostTransfer(code, ret, data, reply, option)) {
27242103316Sopenharmony_ci        return ret;
27342103316Sopenharmony_ci    } else {
27442103316Sopenharmony_ci        return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
27542103316Sopenharmony_ci    }
27642103316Sopenharmony_ci
27742103316Sopenharmony_ci    return UEC_OK;
27842103316Sopenharmony_ci}
27942103316Sopenharmony_ci
28042103316Sopenharmony_ciint32_t UsbServerStub::DoGetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option)
28142103316Sopenharmony_ci{
28242103316Sopenharmony_ci    int32_t functions;
28342103316Sopenharmony_ci    int32_t ret = GetCurrentFunctions(functions);
28442103316Sopenharmony_ci    if (ret != UEC_OK) {
28542103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("GetCurrentFunctions", {0, 0}, {0, 0}, ret);
28642103316Sopenharmony_ci        return ret;
28742103316Sopenharmony_ci    }
28842103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, functions, UEC_SERVICE_WRITE_PARCEL_ERROR);
28942103316Sopenharmony_ci    return UEC_OK;
29042103316Sopenharmony_ci}
29142103316Sopenharmony_ci
29242103316Sopenharmony_ciint32_t UsbServerStub::DoSetCurrentFunctions(MessageParcel &data, MessageParcel &reply, MessageOption &option)
29342103316Sopenharmony_ci{
29442103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "SetCurrentFunctions");
29542103316Sopenharmony_ci    int32_t funcs;
29642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
29742103316Sopenharmony_ci    int32_t ret = SetCurrentFunctions(funcs);
29842103316Sopenharmony_ci    if (ret != UEC_OK) {
29942103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("SetCurrentFunctions", {0, 0}, {0, 0}, ret);
30042103316Sopenharmony_ci    }
30142103316Sopenharmony_ci    return ret;
30242103316Sopenharmony_ci}
30342103316Sopenharmony_ci
30442103316Sopenharmony_ciint32_t UsbServerStub::DoUsbFunctionsFromString(MessageParcel &data, MessageParcel &reply, MessageOption &option)
30542103316Sopenharmony_ci{
30642103316Sopenharmony_ci    std::string funcs;
30742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
30842103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, UsbFunctionsFromString(funcs), UEC_SERVICE_WRITE_PARCEL_ERROR);
30942103316Sopenharmony_ci    return UEC_OK;
31042103316Sopenharmony_ci}
31142103316Sopenharmony_ci
31242103316Sopenharmony_ciint32_t UsbServerStub::DoUsbFunctionsToString(MessageParcel &data, MessageParcel &reply, MessageOption &option)
31342103316Sopenharmony_ci{
31442103316Sopenharmony_ci    int32_t funcs;
31542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, funcs, UEC_SERVICE_READ_PARCEL_ERROR);
31642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, String, UsbFunctionsToString(funcs), UEC_SERVICE_WRITE_PARCEL_ERROR);
31742103316Sopenharmony_ci    return UEC_OK;
31842103316Sopenharmony_ci}
31942103316Sopenharmony_ci
32042103316Sopenharmony_ciint32_t UsbServerStub::DoOpenDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
32142103316Sopenharmony_ci{
32242103316Sopenharmony_ci    uint8_t busNum = 0;
32342103316Sopenharmony_ci    uint8_t devAddr = 0;
32442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
32542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
32642103316Sopenharmony_ci    int32_t ret = OpenDevice(busNum, devAddr);
32742103316Sopenharmony_ci    if (ret != UEC_OK) {
32842103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("OpenDevice", {busNum, devAddr}, {0, 0}, ret);
32942103316Sopenharmony_ci        return ret;
33042103316Sopenharmony_ci    }
33142103316Sopenharmony_ci
33242103316Sopenharmony_ci    return UEC_OK;
33342103316Sopenharmony_ci}
33442103316Sopenharmony_ci
33542103316Sopenharmony_ciint32_t UsbServerStub::DoResetDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
33642103316Sopenharmony_ci{
33742103316Sopenharmony_ci    uint8_t busNum = 0;
33842103316Sopenharmony_ci    uint8_t devAddr = 0;
33942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
34042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
34142103316Sopenharmony_ci    int32_t ret = ResetDevice(busNum, devAddr);
34242103316Sopenharmony_ci    if (ret != UEC_OK) {
34342103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("ResetDevice", {busNum, devAddr}, {0, 0}, ret);
34442103316Sopenharmony_ci        return ret;
34542103316Sopenharmony_ci    }
34642103316Sopenharmony_ci
34742103316Sopenharmony_ci    return UEC_OK;
34842103316Sopenharmony_ci}
34942103316Sopenharmony_ci
35042103316Sopenharmony_ciint32_t UsbServerStub::DoHasRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
35142103316Sopenharmony_ci{
35242103316Sopenharmony_ci    std::u16string deviceName = u"";
35342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
35442103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Bool, HasRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
35542103316Sopenharmony_ci
35642103316Sopenharmony_ci    return UEC_OK;
35742103316Sopenharmony_ci}
35842103316Sopenharmony_ci
35942103316Sopenharmony_ciint32_t UsbServerStub::DoRequestRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
36042103316Sopenharmony_ci{
36142103316Sopenharmony_ci    std::u16string deviceName = u"";
36242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
36342103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, RequestRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
36442103316Sopenharmony_ci    return UEC_OK;
36542103316Sopenharmony_ci}
36642103316Sopenharmony_ci
36742103316Sopenharmony_ciint32_t UsbServerStub::DoRemoveRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
36842103316Sopenharmony_ci{
36942103316Sopenharmony_ci    std::u16string deviceName = u"";
37042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String16, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
37142103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, RemoveRight(Str16ToStr8(deviceName)), UEC_SERVICE_WRITE_PARCEL_ERROR);
37242103316Sopenharmony_ci    return UEC_OK;
37342103316Sopenharmony_ci}
37442103316Sopenharmony_ci
37542103316Sopenharmony_ciint32_t UsbServerStub::DoGetPorts(MessageParcel &data, MessageParcel &reply, MessageOption &option)
37642103316Sopenharmony_ci{
37742103316Sopenharmony_ci    std::vector<UsbPort> ports;
37842103316Sopenharmony_ci    int32_t ret = GetPorts(ports);
37942103316Sopenharmony_ci    USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts ret %{public}d ", ret);
38042103316Sopenharmony_ci    if (ret != UEC_OK) {
38142103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("GetPorts", {0, 0}, {0, 0}, ret);
38242103316Sopenharmony_ci        return ret;
38342103316Sopenharmony_ci    }
38442103316Sopenharmony_ci    uint32_t size = ports.size();
38542103316Sopenharmony_ci    USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts size %{public}d ", size);
38642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, size, UEC_SERVICE_WRITE_PARCEL_ERROR);
38742103316Sopenharmony_ci    for (uint32_t i = 0; i < size; ++i) {
38842103316Sopenharmony_ci        ret = WriteUsbPort(reply, ports[i]);
38942103316Sopenharmony_ci        if (ret) {
39042103316Sopenharmony_ci            return ret;
39142103316Sopenharmony_ci        }
39242103316Sopenharmony_ci    }
39342103316Sopenharmony_ci    return ret;
39442103316Sopenharmony_ci}
39542103316Sopenharmony_ci
39642103316Sopenharmony_ciint32_t UsbServerStub::WriteUsbPort(MessageParcel &reply, const UsbPort &port)
39742103316Sopenharmony_ci{
39842103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, port.id, UEC_SERVICE_WRITE_PARCEL_ERROR);
39942103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, port.supportedModes, UEC_SERVICE_WRITE_PARCEL_ERROR);
40042103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentMode, UEC_SERVICE_WRITE_PARCEL_ERROR);
40142103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentPowerRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
40242103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, port.usbPortStatus.currentDataRole, UEC_SERVICE_WRITE_PARCEL_ERROR);
40342103316Sopenharmony_ci    USB_HILOGI(MODULE_SERVICE, "UsbServerStub::GetPorts supportedModes %{public}d ", port.supportedModes);
40442103316Sopenharmony_ci    return UEC_OK;
40542103316Sopenharmony_ci}
40642103316Sopenharmony_ci
40742103316Sopenharmony_ciint32_t UsbServerStub::DoGetSupportedModes(MessageParcel &data, MessageParcel &reply, MessageOption &option)
40842103316Sopenharmony_ci{
40942103316Sopenharmony_ci    int32_t supportedModes = 0;
41042103316Sopenharmony_ci    int32_t portId = 0;
41142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, portId, UEC_SERVICE_READ_PARCEL_ERROR);
41242103316Sopenharmony_ci    int32_t ret = GetSupportedModes(portId, supportedModes);
41342103316Sopenharmony_ci    if (ret != UEC_OK) {
41442103316Sopenharmony_ci        return ret;
41542103316Sopenharmony_ci    }
41642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, supportedModes, UEC_SERVICE_WRITE_PARCEL_ERROR);
41742103316Sopenharmony_ci    return ret;
41842103316Sopenharmony_ci}
41942103316Sopenharmony_ci
42042103316Sopenharmony_ciint32_t UsbServerStub::DoSetPortRole(MessageParcel &data, MessageParcel &reply, MessageOption &option)
42142103316Sopenharmony_ci{
42242103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "SetPortRole");
42342103316Sopenharmony_ci    int32_t portId = 0;
42442103316Sopenharmony_ci    int32_t powerRole = 0;
42542103316Sopenharmony_ci    int32_t dataRole = 0;
42642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, portId, UEC_SERVICE_READ_PARCEL_ERROR);
42742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, powerRole, UEC_SERVICE_READ_PARCEL_ERROR);
42842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, dataRole, UEC_SERVICE_READ_PARCEL_ERROR);
42942103316Sopenharmony_ci    int32_t ret = SetPortRole(portId, powerRole, dataRole);
43042103316Sopenharmony_ci    if (ret != UEC_OK) {
43142103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("SetPortRole", {0, 0}, {0, 0}, ret);
43242103316Sopenharmony_ci        return ret;
43342103316Sopenharmony_ci    }
43442103316Sopenharmony_ci    return ret;
43542103316Sopenharmony_ci}
43642103316Sopenharmony_ci
43742103316Sopenharmony_ciint32_t UsbServerStub::DoClaimInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
43842103316Sopenharmony_ci{
43942103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "ClaimInterface");
44042103316Sopenharmony_ci    uint8_t busNum = 0;
44142103316Sopenharmony_ci    uint8_t devAddr = 0;
44242103316Sopenharmony_ci    uint8_t interface = 0;
44342103316Sopenharmony_ci    uint8_t force = 0;
44442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
44542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
44642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
44742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, force, UEC_SERVICE_READ_PARCEL_ERROR);
44842103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(
44942103316Sopenharmony_ci        reply, Int32, ClaimInterface(busNum, devAddr, interface, force), UEC_SERVICE_WRITE_PARCEL_ERROR);
45042103316Sopenharmony_ci    return UEC_OK;
45142103316Sopenharmony_ci}
45242103316Sopenharmony_ci
45342103316Sopenharmony_ciint32_t UsbServerStub::DoUsbAttachKernelDriver(MessageParcel &data, MessageParcel &reply, MessageOption &option)
45442103316Sopenharmony_ci{
45542103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "UsbAttachKernelDriver");
45642103316Sopenharmony_ci    uint8_t busNum = 0;
45742103316Sopenharmony_ci    uint8_t devAddr = 0;
45842103316Sopenharmony_ci    uint8_t interface = 0;
45942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
46042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
46142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
46242103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(
46342103316Sopenharmony_ci        reply, Int32, UsbAttachKernelDriver(busNum, devAddr, interface), UEC_SERVICE_WRITE_PARCEL_ERROR);
46442103316Sopenharmony_ci    return UEC_OK;
46542103316Sopenharmony_ci}
46642103316Sopenharmony_ci
46742103316Sopenharmony_ciint32_t UsbServerStub::DoUsbDetachKernelDriver(MessageParcel &data, MessageParcel &reply, MessageOption &option)
46842103316Sopenharmony_ci{
46942103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "UsbDetachKernelDriver");
47042103316Sopenharmony_ci    uint8_t busNum = 0;
47142103316Sopenharmony_ci    uint8_t devAddr = 0;
47242103316Sopenharmony_ci    uint8_t interface = 0;
47342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
47442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
47542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
47642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(
47742103316Sopenharmony_ci        reply, Int32, UsbDetachKernelDriver(busNum, devAddr, interface), UEC_SERVICE_WRITE_PARCEL_ERROR);
47842103316Sopenharmony_ci    return UEC_OK;
47942103316Sopenharmony_ci}
48042103316Sopenharmony_ci
48142103316Sopenharmony_ciint32_t UsbServerStub::DoReleaseInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
48242103316Sopenharmony_ci{
48342103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "ReleaseInterface");
48442103316Sopenharmony_ci    uint8_t busNum = 0;
48542103316Sopenharmony_ci    uint8_t devAddr = 0;
48642103316Sopenharmony_ci    uint8_t interface = 0;
48742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_READ_PARCEL_ERROR);
48842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_READ_PARCEL_ERROR);
48942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_READ_PARCEL_ERROR);
49042103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ReleaseInterface(busNum, devAddr, interface), UEC_SERVICE_WRITE_PARCEL_ERROR);
49142103316Sopenharmony_ci    return UEC_OK;
49242103316Sopenharmony_ci}
49342103316Sopenharmony_ci
49442103316Sopenharmony_ciint32_t UsbServerStub::DoBulkTransferRead(MessageParcel &data, MessageParcel &reply, MessageOption &option)
49542103316Sopenharmony_ci{
49642103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkTransferRead");
49742103316Sopenharmony_ci    uint8_t busNum = 0;
49842103316Sopenharmony_ci    uint8_t devAddr = 0;
49942103316Sopenharmony_ci    uint8_t interface = 0;
50042103316Sopenharmony_ci    uint8_t endpoint = 0;
50142103316Sopenharmony_ci    int32_t timeOut = 0;
50242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
50342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
50442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
50542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
50642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
50742103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
50842103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
50942103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
51042103316Sopenharmony_ci    int32_t ret = BulkTransferRead(tmpDev, tmpPipe, bufferData, timeOut);
51142103316Sopenharmony_ci    if (ret != UEC_OK) {
51242103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "read failed ret:%{public}d", ret);
51342103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("BulkTransferRead", tmpDev, tmpPipe, ret);
51442103316Sopenharmony_ci        return ret;
51542103316Sopenharmony_ci    }
51642103316Sopenharmony_ci    ret = SetBufferMessage(reply, bufferData);
51742103316Sopenharmony_ci    if (ret != UEC_OK) {
51842103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "set buffer failed ret:%{public}d", ret);
51942103316Sopenharmony_ci    }
52042103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
52142103316Sopenharmony_ci    return ret;
52242103316Sopenharmony_ci}
52342103316Sopenharmony_ci
52442103316Sopenharmony_ciint32_t UsbServerStub::DoBulkTransferReadwithLength(MessageParcel &data, MessageParcel &reply, MessageOption &option)
52542103316Sopenharmony_ci{
52642103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkTransferRead");
52742103316Sopenharmony_ci    uint8_t busNum = 0;
52842103316Sopenharmony_ci    uint8_t devAddr = 0;
52942103316Sopenharmony_ci    uint8_t interface = 0;
53042103316Sopenharmony_ci    uint8_t endpoint = 0;
53142103316Sopenharmony_ci    int32_t length = 0;
53242103316Sopenharmony_ci    int32_t timeOut = 0;
53342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
53442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
53542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
53642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
53742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, length, UEC_SERVICE_WRITE_PARCEL_ERROR);
53842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
53942103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
54042103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
54142103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
54242103316Sopenharmony_ci    int32_t ret = BulkTransferReadwithLength(tmpDev, tmpPipe, length, bufferData, timeOut);
54342103316Sopenharmony_ci    if (ret != UEC_OK) {
54442103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "read failed ret:%{public}d", ret);
54542103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("BulkTransferRead", tmpDev, tmpPipe, ret);
54642103316Sopenharmony_ci        return ret;
54742103316Sopenharmony_ci    }
54842103316Sopenharmony_ci    ret = SetBufferMessage(reply, bufferData);
54942103316Sopenharmony_ci    if (ret != UEC_OK) {
55042103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "set buffer failed ret:%{public}d", ret);
55142103316Sopenharmony_ci    }
55242103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
55342103316Sopenharmony_ci    return ret;
55442103316Sopenharmony_ci}
55542103316Sopenharmony_ci
55642103316Sopenharmony_ciint32_t UsbServerStub::DoBulkTransferWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option)
55742103316Sopenharmony_ci{
55842103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkTransferWrite");
55942103316Sopenharmony_ci    uint8_t busNum = 0;
56042103316Sopenharmony_ci    uint8_t devAddr = 0;
56142103316Sopenharmony_ci    uint8_t interface = 0;
56242103316Sopenharmony_ci    uint8_t endpoint = 0;
56342103316Sopenharmony_ci    int32_t timeOut = 0;
56442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
56542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
56642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
56742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
56842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
56942103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
57042103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
57142103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
57242103316Sopenharmony_ci    int32_t ret = GetBufferMessage(data, bufferData);
57342103316Sopenharmony_ci    if (ret != UEC_OK) {
57442103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "GetBufferMessage failedret:%{public}d", ret);
57542103316Sopenharmony_ci        return ret;
57642103316Sopenharmony_ci    }
57742103316Sopenharmony_ci    ret = BulkTransferWrite(tmpDev, tmpPipe, bufferData, timeOut);
57842103316Sopenharmony_ci    if (ret != UEC_OK) {
57942103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "BulkTransferWrite error ret:%{public}d", ret);
58042103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("BulkTransferWrite", tmpDev, tmpPipe, ret);
58142103316Sopenharmony_ci    }
58242103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
58342103316Sopenharmony_ci    return ret;
58442103316Sopenharmony_ci}
58542103316Sopenharmony_ci
58642103316Sopenharmony_ciint32_t UsbServerStub::DoControlTransfer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
58742103316Sopenharmony_ci{
58842103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "ControlTransfer");
58942103316Sopenharmony_ci    uint8_t busNum = 0;
59042103316Sopenharmony_ci    uint8_t devAddr = 0;
59142103316Sopenharmony_ci    int32_t requestType;
59242103316Sopenharmony_ci    int32_t request;
59342103316Sopenharmony_ci    int32_t value;
59442103316Sopenharmony_ci    int32_t index;
59542103316Sopenharmony_ci    int32_t timeOut;
59642103316Sopenharmony_ci
59742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
59842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
59942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
60042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, request, UEC_SERVICE_WRITE_PARCEL_ERROR);
60142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, value, UEC_SERVICE_WRITE_PARCEL_ERROR);
60242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, index, UEC_SERVICE_WRITE_PARCEL_ERROR);
60342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
60442103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
60542103316Sopenharmony_ci    int32_t ret = GetBufferMessage(data, bufferData);
60642103316Sopenharmony_ci    if (ret != UEC_OK) {
60742103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "get error ret:%{public}d", ret);
60842103316Sopenharmony_ci        return ret;
60942103316Sopenharmony_ci    }
61042103316Sopenharmony_ci
61142103316Sopenharmony_ci    bool bWrite = ((static_cast<uint32_t>(requestType) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
61242103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
61342103316Sopenharmony_ci    const UsbCtrlTransfer tctrl = {requestType, request, value, index, timeOut};
61442103316Sopenharmony_ci    ret = ControlTransfer(tmpDev, tctrl, bufferData);
61542103316Sopenharmony_ci    if (ret != UEC_OK) {
61642103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("ControlTransfer", tmpDev, {0, 0}, ret);
61742103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ControlTransfer error ret:%{public}d", ret);
61842103316Sopenharmony_ci        return ret;
61942103316Sopenharmony_ci    }
62042103316Sopenharmony_ci
62142103316Sopenharmony_ci    if (!bWrite) {
62242103316Sopenharmony_ci        ret = SetBufferMessage(reply, bufferData);
62342103316Sopenharmony_ci        if (ret != UEC_OK) {
62442103316Sopenharmony_ci            USB_HILOGE(MODULE_USBD, "Set buffer message error length = %{public}d", ret);
62542103316Sopenharmony_ci        }
62642103316Sopenharmony_ci    }
62742103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
62842103316Sopenharmony_ci    return UEC_OK;
62942103316Sopenharmony_ci}
63042103316Sopenharmony_ci
63142103316Sopenharmony_ciint32_t UsbServerStub::DoUsbControlTransfer(MessageParcel &data, MessageParcel &reply, MessageOption &option)
63242103316Sopenharmony_ci{
63342103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "ControlTransfer");
63442103316Sopenharmony_ci    uint8_t busNum = 0;
63542103316Sopenharmony_ci    uint8_t devAddr = 0;
63642103316Sopenharmony_ci    int32_t requestType;
63742103316Sopenharmony_ci    int32_t request;
63842103316Sopenharmony_ci    int32_t value;
63942103316Sopenharmony_ci    int32_t index;
64042103316Sopenharmony_ci    int32_t length;
64142103316Sopenharmony_ci    int32_t timeOut;
64242103316Sopenharmony_ci
64342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
64442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
64542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, requestType, UEC_SERVICE_WRITE_PARCEL_ERROR);
64642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, request, UEC_SERVICE_WRITE_PARCEL_ERROR);
64742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, value, UEC_SERVICE_WRITE_PARCEL_ERROR);
64842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, index, UEC_SERVICE_WRITE_PARCEL_ERROR);
64942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, length, UEC_SERVICE_WRITE_PARCEL_ERROR);
65042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
65142103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
65242103316Sopenharmony_ci    int32_t ret = GetBufferMessage(data, bufferData);
65342103316Sopenharmony_ci    if (ret != UEC_OK) {
65442103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "get error ret:%{public}d", ret);
65542103316Sopenharmony_ci        return ret;
65642103316Sopenharmony_ci    }
65742103316Sopenharmony_ci
65842103316Sopenharmony_ci    bool bWrite = ((static_cast<uint32_t>(requestType) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
65942103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
66042103316Sopenharmony_ci    const UsbCtrlTransferParams tctrlParams = {requestType, request, value, index, length, timeOut};
66142103316Sopenharmony_ci    ret = UsbControlTransfer(tmpDev, tctrlParams, bufferData);
66242103316Sopenharmony_ci    if (ret != UEC_OK) {
66342103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("ControlTransfer", tmpDev, {0, 0}, ret);
66442103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ControlTransfer error ret:%{public}d", ret);
66542103316Sopenharmony_ci        return ret;
66642103316Sopenharmony_ci    }
66742103316Sopenharmony_ci
66842103316Sopenharmony_ci    if (!bWrite) {
66942103316Sopenharmony_ci        ret = SetBufferMessage(reply, bufferData);
67042103316Sopenharmony_ci        if (ret != UEC_OK) {
67142103316Sopenharmony_ci            USB_HILOGE(MODULE_USBD, "Set buffer message error length = %{public}d", ret);
67242103316Sopenharmony_ci        }
67342103316Sopenharmony_ci    }
67442103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
67542103316Sopenharmony_ci    return UEC_OK;
67642103316Sopenharmony_ci}
67742103316Sopenharmony_ci
67842103316Sopenharmony_ciint32_t UsbServerStub::DoSetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
67942103316Sopenharmony_ci{
68042103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "SetActiveConfig");
68142103316Sopenharmony_ci    uint8_t busNum = 0;
68242103316Sopenharmony_ci    uint8_t devAddr = 0;
68342103316Sopenharmony_ci    uint8_t config = 0;
68442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
68542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
68642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, config, UEC_SERVICE_WRITE_PARCEL_ERROR);
68742103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, SetActiveConfig(busNum, devAddr, config), UEC_SERVICE_WRITE_PARCEL_ERROR);
68842103316Sopenharmony_ci    return UEC_OK;
68942103316Sopenharmony_ci}
69042103316Sopenharmony_ci
69142103316Sopenharmony_ciint32_t UsbServerStub::DoGetActiveConfig(MessageParcel &data, MessageParcel &reply, MessageOption &option)
69242103316Sopenharmony_ci{
69342103316Sopenharmony_ci    uint8_t busNum = 0;
69442103316Sopenharmony_ci    uint8_t devAddr = 0;
69542103316Sopenharmony_ci    uint8_t config = 0;
69642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
69742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
69842103316Sopenharmony_ci    int32_t ret = GetActiveConfig(busNum, devAddr, config);
69942103316Sopenharmony_ci    if (ret == UEC_OK) {
70042103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(reply, Uint8, config, UEC_SERVICE_WRITE_PARCEL_ERROR);
70142103316Sopenharmony_ci    }
70242103316Sopenharmony_ci    return ret;
70342103316Sopenharmony_ci}
70442103316Sopenharmony_ci
70542103316Sopenharmony_ciint32_t UsbServerStub::DoSetInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
70642103316Sopenharmony_ci{
70742103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "SetInterface");
70842103316Sopenharmony_ci    uint8_t busNum = 0;
70942103316Sopenharmony_ci    uint8_t devAddr = 0;
71042103316Sopenharmony_ci    uint8_t interfaceId = 0;
71142103316Sopenharmony_ci    uint8_t altIndex = 0;
71242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
71342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
71442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
71542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, altIndex, UEC_SERVICE_WRITE_PARCEL_ERROR);
71642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(
71742103316Sopenharmony_ci        reply, Int32, SetInterface(busNum, devAddr, interfaceId, altIndex), UEC_SERVICE_WRITE_PARCEL_ERROR);
71842103316Sopenharmony_ci    return UEC_OK;
71942103316Sopenharmony_ci}
72042103316Sopenharmony_ci
72142103316Sopenharmony_ciint32_t UsbServerStub::DoGetRawDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option)
72242103316Sopenharmony_ci{
72342103316Sopenharmony_ci    uint8_t busNum = 0;
72442103316Sopenharmony_ci    uint8_t devAddr = 0;
72542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
72642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
72742103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
72842103316Sopenharmony_ci    int32_t ret = GetRawDescriptor(busNum, devAddr, bufferData);
72942103316Sopenharmony_ci    if (ret == UEC_OK) {
73042103316Sopenharmony_ci        ret = SetBufferMessage(reply, bufferData);
73142103316Sopenharmony_ci        if (ret != UEC_OK) {
73242103316Sopenharmony_ci            USB_HILOGE(MODULE_USBD, "SetBufferMessage failed ret:%{public}d", ret);
73342103316Sopenharmony_ci        }
73442103316Sopenharmony_ci    } else {
73542103316Sopenharmony_ci        USB_HILOGW(MODULE_USBD, "GetRawDescriptor failed ret:%{public}d", ret);
73642103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("GetRawDescriptor", {busNum, devAddr}, {0, 0}, ret);
73742103316Sopenharmony_ci    }
73842103316Sopenharmony_ci    return ret;
73942103316Sopenharmony_ci}
74042103316Sopenharmony_ci
74142103316Sopenharmony_ciint32_t UsbServerStub::DoGetFileDescriptor(MessageParcel &data, MessageParcel &reply, MessageOption &option)
74242103316Sopenharmony_ci{
74342103316Sopenharmony_ci    uint8_t busNum = 0;
74442103316Sopenharmony_ci    uint8_t devAddr = 0;
74542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
74642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
74742103316Sopenharmony_ci    int32_t fd = -1;
74842103316Sopenharmony_ci    int32_t ret = GetFileDescriptor(busNum, devAddr, fd);
74942103316Sopenharmony_ci    if (ret == UEC_OK) {
75042103316Sopenharmony_ci        if (!WriteFileDescriptor(reply, fd)) {
75142103316Sopenharmony_ci            USB_HILOGW(MODULE_USB_SERVICE, "%{public}s: write fd failed!", __func__);
75242103316Sopenharmony_ci            return UEC_INTERFACE_WRITE_PARCEL_ERROR;
75342103316Sopenharmony_ci        }
75442103316Sopenharmony_ci    } else {
75542103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
75642103316Sopenharmony_ci    }
75742103316Sopenharmony_ci    return ret;
75842103316Sopenharmony_ci}
75942103316Sopenharmony_ci
76042103316Sopenharmony_ciint32_t UsbServerStub::DoRequestQueue(MessageParcel &data, MessageParcel &reply, MessageOption &option)
76142103316Sopenharmony_ci{
76242103316Sopenharmony_ci    uint8_t busNum = 0;
76342103316Sopenharmony_ci    uint8_t devAddr = 0;
76442103316Sopenharmony_ci    uint8_t ifId = 0;
76542103316Sopenharmony_ci    uint8_t endpoint = 0;
76642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
76742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
76842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, ifId, UEC_SERVICE_WRITE_PARCEL_ERROR);
76942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
77042103316Sopenharmony_ci    std::vector<uint8_t> clientData;
77142103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
77242103316Sopenharmony_ci
77342103316Sopenharmony_ci    int32_t ret = UsbServerStub::GetBufferMessage(data, clientData);
77442103316Sopenharmony_ci    if (ret != UEC_OK) {
77542103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed  ret:%{public}d", ret);
77642103316Sopenharmony_ci        return ret;
77742103316Sopenharmony_ci    }
77842103316Sopenharmony_ci    ret = UsbServerStub::GetBufferMessage(data, bufferData);
77942103316Sopenharmony_ci    if (ret != UEC_OK) {
78042103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed  ret:%{public}d", ret);
78142103316Sopenharmony_ci        return ret;
78242103316Sopenharmony_ci    }
78342103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
78442103316Sopenharmony_ci    const UsbPipe tmpPipe = {ifId, endpoint};
78542103316Sopenharmony_ci    ret = RequestQueue(tmpDev, tmpPipe, clientData, bufferData);
78642103316Sopenharmony_ci    if (ret != UEC_OK) {
78742103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "GetBufferMessage failed ret:%{public}d", ret);
78842103316Sopenharmony_ci    }
78942103316Sopenharmony_ci    return ret;
79042103316Sopenharmony_ci}
79142103316Sopenharmony_ci
79242103316Sopenharmony_ciint32_t UsbServerStub::DoRequestWait(MessageParcel &data, MessageParcel &reply, MessageOption &option)
79342103316Sopenharmony_ci{
79442103316Sopenharmony_ci    uint8_t busNum = 0;
79542103316Sopenharmony_ci    uint8_t devAddr = 0;
79642103316Sopenharmony_ci    int32_t timeOut = 0;
79742103316Sopenharmony_ci    std::vector<uint8_t> clientData;
79842103316Sopenharmony_ci    std::vector<uint8_t> bufferData;
79942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
80042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
80142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, timeOut, UEC_SERVICE_WRITE_PARCEL_ERROR);
80242103316Sopenharmony_ci
80342103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
80442103316Sopenharmony_ci    int32_t ret = RequestWait(tmpDev, timeOut, clientData, bufferData);
80542103316Sopenharmony_ci    if (ret != UEC_OK) {
80642103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "RequestWait failed ret:%{public}d", ret);
80742103316Sopenharmony_ci        return ret;
80842103316Sopenharmony_ci    }
80942103316Sopenharmony_ci
81042103316Sopenharmony_ci    ret = SetBufferMessage(reply, clientData);
81142103316Sopenharmony_ci    if (ret != UEC_OK) {
81242103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "Set clientData failed ret:%{public}d", ret);
81342103316Sopenharmony_ci        return ret;
81442103316Sopenharmony_ci    }
81542103316Sopenharmony_ci
81642103316Sopenharmony_ci    ret = SetBufferMessage(reply, bufferData);
81742103316Sopenharmony_ci    if (ret != UEC_OK) {
81842103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "Set bufferData failed ret:%{public}d", ret);
81942103316Sopenharmony_ci        return ret;
82042103316Sopenharmony_ci    }
82142103316Sopenharmony_ci    return ret;
82242103316Sopenharmony_ci}
82342103316Sopenharmony_ci
82442103316Sopenharmony_ciint32_t UsbServerStub::DoRequestCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option)
82542103316Sopenharmony_ci{
82642103316Sopenharmony_ci    uint8_t busNum = 0;
82742103316Sopenharmony_ci    uint8_t devAddr = 0;
82842103316Sopenharmony_ci    uint8_t interfaceId = 0;
82942103316Sopenharmony_ci    uint8_t endpointId = 0;
83042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
83142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
83242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
83342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
83442103316Sopenharmony_ci    int32_t ret = RequestCancel(busNum, devAddr, interfaceId, endpointId);
83542103316Sopenharmony_ci    if (ret != UEC_OK) {
83642103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "failed ret:%{public}d", ret);
83742103316Sopenharmony_ci    }
83842103316Sopenharmony_ci    return ret;
83942103316Sopenharmony_ci}
84042103316Sopenharmony_ci
84142103316Sopenharmony_ciint32_t UsbServerStub::DoClose(MessageParcel &data, MessageParcel &reply, MessageOption &option)
84242103316Sopenharmony_ci{
84342103316Sopenharmony_ci    uint8_t busNum = 0;
84442103316Sopenharmony_ci    uint8_t devAddr = 0;
84542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
84642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
84742103316Sopenharmony_ci    int32_t ret = Close(busNum, devAddr);
84842103316Sopenharmony_ci    if (ret != UEC_OK) {
84942103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "failed ret:%{public}d", ret);
85042103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("CloseDevice", {busNum, devAddr}, {0, 0}, ret);
85142103316Sopenharmony_ci    }
85242103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
85342103316Sopenharmony_ci    return ret;
85442103316Sopenharmony_ci}
85542103316Sopenharmony_ci
85642103316Sopenharmony_ciint32_t UsbServerStub::DoGetDevices(MessageParcel &data, MessageParcel &reply, MessageOption &option)
85742103316Sopenharmony_ci{
85842103316Sopenharmony_ci    std::vector<UsbDevice> deviceList;
85942103316Sopenharmony_ci    int32_t ret = GetDevices(deviceList);
86042103316Sopenharmony_ci    if (ret != UEC_OK) {
86142103316Sopenharmony_ci        USB_HILOGE(MODULE_SERVICE, "GetDevices failed ret = %{public}d", ret);
86242103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("GetDevices", {0, 0}, {0, 0}, ret);
86342103316Sopenharmony_ci        return ret;
86442103316Sopenharmony_ci    }
86542103316Sopenharmony_ci    USB_HILOGI(MODULE_SERVICE, "list size = %{public}zu", deviceList.size());
86642103316Sopenharmony_ci    ret = SetDeviceListMessageParcel(deviceList, reply);
86742103316Sopenharmony_ci    if (ret != UEC_OK) {
86842103316Sopenharmony_ci        USB_HILOGE(MODULE_USB_INNERKIT, "SetDeviceListMessageParcel failed ret:%{public}d", ret);
86942103316Sopenharmony_ci    }
87042103316Sopenharmony_ci    return ret;
87142103316Sopenharmony_ci}
87242103316Sopenharmony_ci
87342103316Sopenharmony_ciint32_t UsbServerStub::SetDeviceListMessageParcel(std::vector<UsbDevice> &deviceList, MessageParcel &data)
87442103316Sopenharmony_ci{
87542103316Sopenharmony_ci    int32_t deviceCount = (int32_t)deviceList.size();
87642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, deviceCount, UEC_SERVICE_WRITE_PARCEL_ERROR);
87742103316Sopenharmony_ci    for (int32_t i = 0; i < deviceCount; ++i) {
87842103316Sopenharmony_ci        UsbDevice &devInfo = deviceList[i];
87942103316Sopenharmony_ci        int32_t ret = SetDeviceMessageParcel(devInfo, data);
88042103316Sopenharmony_ci        if (ret) {
88142103316Sopenharmony_ci            return ret;
88242103316Sopenharmony_ci        }
88342103316Sopenharmony_ci    }
88442103316Sopenharmony_ci    return UEC_OK;
88542103316Sopenharmony_ci}
88642103316Sopenharmony_ci
88742103316Sopenharmony_ciint32_t UsbServerStub::SetDeviceMessageParcel(UsbDevice &devInfo, MessageParcel &data)
88842103316Sopenharmony_ci{
88942103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetBusNum(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89042103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetDevAddr(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89142103316Sopenharmony_ci
89242103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetVendorId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89342103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetProductId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89442103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89542103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetSubclass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetProtocol(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89742103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiManufacturer(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89842103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiProduct(), UEC_SERVICE_WRITE_PARCEL_ERROR);
89942103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetiSerialNumber(), UEC_SERVICE_WRITE_PARCEL_ERROR);
90042103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Uint8, devInfo.GetbMaxPacketSize0(), UEC_SERVICE_WRITE_PARCEL_ERROR);
90142103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Uint16, devInfo.GetbcdUSB(), UEC_SERVICE_WRITE_PARCEL_ERROR);
90242103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Uint16, devInfo.GetbcdDevice(), UEC_SERVICE_WRITE_PARCEL_ERROR);
90342103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
90442103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetManufacturerName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
90542103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetProductName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
90642103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetVersion()), UEC_SERVICE_WRITE_PARCEL_ERROR);
90742103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(devInfo.GetmSerial()), UEC_SERVICE_WRITE_PARCEL_ERROR);
90842103316Sopenharmony_ci
90942103316Sopenharmony_ci    USB_HILOGE(MODULE_USB_INNERKIT, "devInfo:%{public}s", devInfo.ToString().c_str());
91042103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(data, Int32, devInfo.GetConfigCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
91142103316Sopenharmony_ci    return SetDeviceConfigsMessageParcel(devInfo.GetConfigs(), data);
91242103316Sopenharmony_ci}
91342103316Sopenharmony_ci
91442103316Sopenharmony_ciint32_t UsbServerStub::SetDeviceConfigsMessageParcel(std::vector<USBConfig> &configs, MessageParcel &data)
91542103316Sopenharmony_ci{
91642103316Sopenharmony_ci    for (auto it = configs.begin(); it != configs.end(); ++it) {
91742103316Sopenharmony_ci        USBConfig config = *it;
91842103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, config.GetId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
91942103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Uint32, config.GetAttributes(), UEC_SERVICE_WRITE_PARCEL_ERROR);
92042103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, config.GetMaxPower(), UEC_SERVICE_WRITE_PARCEL_ERROR);
92142103316Sopenharmony_ci
92242103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Uint8, config.GetiConfiguration(), UEC_SERVICE_WRITE_PARCEL_ERROR);
92342103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(config.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
92442103316Sopenharmony_ci
92542103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Uint32, config.GetInterfaceCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
92642103316Sopenharmony_ci        USB_HILOGI(MODULE_USB_SERVICE, "devInfo=%{public}s", config.ToString().c_str());
92742103316Sopenharmony_ci        int32_t ret = SetDeviceInterfacesMessageParcel(config.GetInterfaces(), data);
92842103316Sopenharmony_ci        if (ret) {
92942103316Sopenharmony_ci            return ret;
93042103316Sopenharmony_ci        }
93142103316Sopenharmony_ci    }
93242103316Sopenharmony_ci    return UEC_OK;
93342103316Sopenharmony_ci}
93442103316Sopenharmony_ci
93542103316Sopenharmony_ciint32_t UsbServerStub::SetDeviceInterfacesMessageParcel(std::vector<UsbInterface> &interfaces, MessageParcel &data)
93642103316Sopenharmony_ci{
93742103316Sopenharmony_ci    for (auto it = interfaces.begin(); it != interfaces.end(); ++it) {
93842103316Sopenharmony_ci        UsbInterface interface = *it;
93942103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, interface.GetId(), UEC_SERVICE_WRITE_PARCEL_ERROR);
94042103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, interface.GetClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
94142103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, interface.GetSubClass(), UEC_SERVICE_WRITE_PARCEL_ERROR);
94242103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, interface.GetAlternateSetting(), UEC_SERVICE_WRITE_PARCEL_ERROR);
94342103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, interface.GetProtocol(), UEC_SERVICE_WRITE_PARCEL_ERROR);
94442103316Sopenharmony_ci
94542103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Uint8, interface.GetiInterface(), UEC_SERVICE_WRITE_PARCEL_ERROR);
94642103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, String16, Str8ToStr16(interface.GetName()), UEC_SERVICE_WRITE_PARCEL_ERROR);
94742103316Sopenharmony_ci
94842103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, interface.GetEndpointCount(), UEC_SERVICE_WRITE_PARCEL_ERROR);
94942103316Sopenharmony_ci        USB_HILOGI(MODULE_USB_SERVICE, "interface=%{public}s", interface.ToString().c_str());
95042103316Sopenharmony_ci        int32_t ret = SetDeviceEndpointsMessageParcel(interface.GetEndpoints(), data);
95142103316Sopenharmony_ci        if (ret) {
95242103316Sopenharmony_ci            return ret;
95342103316Sopenharmony_ci        }
95442103316Sopenharmony_ci    }
95542103316Sopenharmony_ci    return UEC_OK;
95642103316Sopenharmony_ci}
95742103316Sopenharmony_ci
95842103316Sopenharmony_ciint32_t UsbServerStub::SetDeviceEndpointsMessageParcel(std::vector<USBEndpoint> &eps, MessageParcel &data)
95942103316Sopenharmony_ci{
96042103316Sopenharmony_ci    for (auto it = eps.begin(); it != eps.end(); ++it) {
96142103316Sopenharmony_ci        USBEndpoint ep = *it;
96242103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Uint32, ep.GetAddress(), UEC_SERVICE_WRITE_PARCEL_ERROR);
96342103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Uint32, ep.GetAttributes(), UEC_SERVICE_WRITE_PARCEL_ERROR);
96442103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, ep.GetInterval(), UEC_SERVICE_WRITE_PARCEL_ERROR);
96542103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(data, Int32, ep.GetMaxPacketSize(), UEC_SERVICE_WRITE_PARCEL_ERROR);
96642103316Sopenharmony_ci        USB_HILOGI(MODULE_USB_SERVICE, "ep=%{public}s", ep.ToString().c_str());
96742103316Sopenharmony_ci    }
96842103316Sopenharmony_ci    return UEC_OK;
96942103316Sopenharmony_ci}
97042103316Sopenharmony_ci
97142103316Sopenharmony_ciint32_t UsbServerStub::DoRegBulkCallback(MessageParcel &data, MessageParcel &reply, MessageOption &option)
97242103316Sopenharmony_ci{
97342103316Sopenharmony_ci    uint8_t busNum = 0;
97442103316Sopenharmony_ci    uint8_t devAddr = 0;
97542103316Sopenharmony_ci    uint8_t interface = 0;
97642103316Sopenharmony_ci    uint8_t endpoint = 0;
97742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
97842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
97942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
98042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
98142103316Sopenharmony_ci    const sptr<IRemoteObject> cb = data.ReadRemoteObject();
98242103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
98342103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
98442103316Sopenharmony_ci    int32_t ret = RegBulkCallback(tmpDev, tmpPipe, cb);
98542103316Sopenharmony_ci    if (ret != UEC_OK) {
98642103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
98742103316Sopenharmony_ci        return ret;
98842103316Sopenharmony_ci    }
98942103316Sopenharmony_ci    return ret;
99042103316Sopenharmony_ci}
99142103316Sopenharmony_ci
99242103316Sopenharmony_ciint32_t UsbServerStub::DoUnRegBulkCallback(MessageParcel &data, MessageParcel &reply, MessageOption &option)
99342103316Sopenharmony_ci{
99442103316Sopenharmony_ci    uint8_t busNum = 0;
99542103316Sopenharmony_ci    uint8_t devAddr = 0;
99642103316Sopenharmony_ci    uint8_t interface = 0;
99742103316Sopenharmony_ci    uint8_t endpoint = 0;
99842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
99942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
100042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
100142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
100242103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
100342103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
100442103316Sopenharmony_ci    int32_t ret = UnRegBulkCallback(tmpDev, tmpPipe);
100542103316Sopenharmony_ci    if (ret != UEC_OK) {
100642103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
100742103316Sopenharmony_ci        return ret;
100842103316Sopenharmony_ci    }
100942103316Sopenharmony_ci    return ret;
101042103316Sopenharmony_ci}
101142103316Sopenharmony_ci
101242103316Sopenharmony_ciint32_t UsbServerStub::DoBulkRead(MessageParcel &data, MessageParcel &reply, MessageOption &option)
101342103316Sopenharmony_ci{
101442103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkRead");
101542103316Sopenharmony_ci    uint8_t busNum = 0;
101642103316Sopenharmony_ci    uint8_t devAddr = 0;
101742103316Sopenharmony_ci    uint8_t interface = 0;
101842103316Sopenharmony_ci    uint8_t endpoint = 0;
101942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
102042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
102142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
102242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
102342103316Sopenharmony_ci    sptr<Ashmem> ashmem = data.ReadAshmem();
102442103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
102542103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
102642103316Sopenharmony_ci    int32_t ret = BulkRead(tmpDev, tmpPipe, ashmem);
102742103316Sopenharmony_ci    if (ret != UEC_OK) {
102842103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "BulkRead failed ret:%{public}d", ret);
102942103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("BulkRead", tmpDev, tmpPipe, ret);
103042103316Sopenharmony_ci        return ret;
103142103316Sopenharmony_ci    }
103242103316Sopenharmony_ci    return ret;
103342103316Sopenharmony_ci}
103442103316Sopenharmony_ci
103542103316Sopenharmony_ciint32_t UsbServerStub::DoBulkWrite(MessageParcel &data, MessageParcel &reply, MessageOption &option)
103642103316Sopenharmony_ci{
103742103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "BulkWrite");
103842103316Sopenharmony_ci    uint8_t busNum = 0;
103942103316Sopenharmony_ci    uint8_t devAddr = 0;
104042103316Sopenharmony_ci    uint8_t interface = 0;
104142103316Sopenharmony_ci    uint8_t endpoint = 0;
104242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
104342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
104442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
104542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
104642103316Sopenharmony_ci    sptr<Ashmem> ashmem = data.ReadAshmem();
104742103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
104842103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
104942103316Sopenharmony_ci    int32_t ret = BulkWrite(tmpDev, tmpPipe, ashmem);
105042103316Sopenharmony_ci    if (ret != UEC_OK) {
105142103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
105242103316Sopenharmony_ci        UsbReportSysEvent::ReportTransforFaultSysEvent("BulkWrite", tmpDev, tmpPipe, ret);
105342103316Sopenharmony_ci        return ret;
105442103316Sopenharmony_ci    }
105542103316Sopenharmony_ci    return ret;
105642103316Sopenharmony_ci}
105742103316Sopenharmony_ci
105842103316Sopenharmony_ciint32_t UsbServerStub::DoBulkCancel(MessageParcel &data, MessageParcel &reply, MessageOption &option)
105942103316Sopenharmony_ci{
106042103316Sopenharmony_ci    uint8_t busNum = 0;
106142103316Sopenharmony_ci    uint8_t devAddr = 0;
106242103316Sopenharmony_ci    uint8_t interface = 0;
106342103316Sopenharmony_ci    uint8_t endpoint = 0;
106442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
106542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
106642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interface, UEC_SERVICE_WRITE_PARCEL_ERROR);
106742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpoint, UEC_SERVICE_WRITE_PARCEL_ERROR);
106842103316Sopenharmony_ci    const UsbDev tmpDev = {busNum, devAddr};
106942103316Sopenharmony_ci    const UsbPipe tmpPipe = {interface, endpoint};
107042103316Sopenharmony_ci    int32_t ret = BulkCancel(tmpDev, tmpPipe);
107142103316Sopenharmony_ci    if (ret != UEC_OK) {
107242103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
107342103316Sopenharmony_ci        return ret;
107442103316Sopenharmony_ci    }
107542103316Sopenharmony_ci    return ret;
107642103316Sopenharmony_ci}
107742103316Sopenharmony_ci
107842103316Sopenharmony_ciint32_t UsbServerStub::DoAddRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
107942103316Sopenharmony_ci{
108042103316Sopenharmony_ci    std::string bundleName;
108142103316Sopenharmony_ci    std::string deviceName;
108242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String, bundleName, UEC_SERVICE_READ_PARCEL_ERROR);
108342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
108442103316Sopenharmony_ci    int32_t ret = AddRight(bundleName, deviceName);
108542103316Sopenharmony_ci    if (ret != UEC_OK) {
108642103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
108742103316Sopenharmony_ci    }
108842103316Sopenharmony_ci    return ret;
108942103316Sopenharmony_ci}
109042103316Sopenharmony_ci
109142103316Sopenharmony_ciint32_t UsbServerStub::DoAddAccessRight(MessageParcel &data, MessageParcel &reply, MessageOption &option)
109242103316Sopenharmony_ci{
109342103316Sopenharmony_ci    std::string tokenId;
109442103316Sopenharmony_ci    std::string deviceName;
109542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String, tokenId, UEC_SERVICE_READ_PARCEL_ERROR);
109642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_READ_PARCEL_ERROR);
109742103316Sopenharmony_ci    int32_t ret = AddAccessRight(tokenId, deviceName);
109842103316Sopenharmony_ci    if (ret != UEC_OK) {
109942103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
110042103316Sopenharmony_ci    }
110142103316Sopenharmony_ci    return ret;
110242103316Sopenharmony_ci}
110342103316Sopenharmony_ci
110442103316Sopenharmony_ciint32_t UsbServerStub::DoManageGlobalInterface(MessageParcel &data, MessageParcel &reply, MessageOption &option)
110542103316Sopenharmony_ci{
110642103316Sopenharmony_ci    bool disable = false;
110742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR);
110842103316Sopenharmony_ci    int32_t ret = ManageGlobalInterface(disable);
110942103316Sopenharmony_ci    if (ret != UEC_OK) {
111042103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
111142103316Sopenharmony_ci    }
111242103316Sopenharmony_ci    return ret;
111342103316Sopenharmony_ci}
111442103316Sopenharmony_ci
111542103316Sopenharmony_ciint32_t UsbServerStub::DoManageDevice(MessageParcel &data, MessageParcel &reply, MessageOption &option)
111642103316Sopenharmony_ci{
111742103316Sopenharmony_ci    int32_t vendorId = 0;
111842103316Sopenharmony_ci    int32_t productId = 0;
111942103316Sopenharmony_ci    bool disable = false;
112042103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, vendorId, UEC_SERVICE_READ_PARCEL_ERROR);
112142103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, productId, UEC_SERVICE_READ_PARCEL_ERROR);
112242103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR);
112342103316Sopenharmony_ci    int32_t ret = ManageDevice(vendorId, productId, disable);
112442103316Sopenharmony_ci    if (ret != UEC_OK) {
112542103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
112642103316Sopenharmony_ci    }
112742103316Sopenharmony_ci    return ret;
112842103316Sopenharmony_ci}
112942103316Sopenharmony_ci
113042103316Sopenharmony_ciint32_t UsbServerStub::DoManageInterfaceType(MessageParcel &data, MessageParcel &reply, MessageOption &option)
113142103316Sopenharmony_ci{
113242103316Sopenharmony_ci    int32_t count;
113342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Int32, count, UEC_SERVICE_READ_PARCEL_ERROR);
113442103316Sopenharmony_ci    bool disable = false;
113542103316Sopenharmony_ci    std::vector<UsbDeviceType> disableType;
113642103316Sopenharmony_ci    if (count > MAX_EDM_LIST_SIZE) {
113742103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "count:%{public}d", count);
113842103316Sopenharmony_ci        return UEC_SERVICE_READ_PARCEL_ERROR;
113942103316Sopenharmony_ci    }
114042103316Sopenharmony_ci    for (int32_t i = 0; i < count; ++i) {
114142103316Sopenharmony_ci        UsbDeviceType usbDeviceType;
114242103316Sopenharmony_ci        READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.baseClass, UEC_SERVICE_READ_PARCEL_ERROR);
114342103316Sopenharmony_ci        READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.subClass, UEC_SERVICE_READ_PARCEL_ERROR);
114442103316Sopenharmony_ci        READ_PARCEL_WITH_RET(data, Int32, usbDeviceType.protocol, UEC_SERVICE_READ_PARCEL_ERROR);
114542103316Sopenharmony_ci        READ_PARCEL_WITH_RET(data, Bool, usbDeviceType.isDeviceType, UEC_SERVICE_READ_PARCEL_ERROR);
114642103316Sopenharmony_ci        disableType.emplace_back(usbDeviceType);
114742103316Sopenharmony_ci    }
114842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Bool, disable, UEC_SERVICE_READ_PARCEL_ERROR);
114942103316Sopenharmony_ci    int32_t ret = ManageInterfaceType(disableType, disable);
115042103316Sopenharmony_ci    if (ret != UEC_OK) {
115142103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
115242103316Sopenharmony_ci    }
115342103316Sopenharmony_ci    return ret;
115442103316Sopenharmony_ci}
115542103316Sopenharmony_ci
115642103316Sopenharmony_ciint32_t UsbServerStub::DoClearHalt(MessageParcel &data, MessageParcel &reply, MessageOption &option)
115742103316Sopenharmony_ci{
115842103316Sopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_USB, "ClearHalt");
115942103316Sopenharmony_ci    uint8_t busNum = 0;
116042103316Sopenharmony_ci    uint8_t devAddr = 0;
116142103316Sopenharmony_ci    uint8_t interfaceId = 0;
116242103316Sopenharmony_ci    uint8_t endpointId = 0;
116342103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
116442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
116542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
116642103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, endpointId, UEC_SERVICE_WRITE_PARCEL_ERROR);
116742103316Sopenharmony_ci    int32_t ret = ClearHalt(busNum, devAddr, interfaceId, endpointId);
116842103316Sopenharmony_ci    WRITE_PARCEL_WITH_RET(reply, Int32, ret, UEC_SERVICE_WRITE_PARCEL_ERROR);
116942103316Sopenharmony_ci    return UEC_OK;
117042103316Sopenharmony_ci}
117142103316Sopenharmony_ci
117242103316Sopenharmony_ciint32_t UsbServerStub::DoGetInterfaceActiveStatus(MessageParcel &data, MessageParcel &reply, MessageOption &option)
117342103316Sopenharmony_ci{
117442103316Sopenharmony_ci    uint8_t busNum = 0;
117542103316Sopenharmony_ci    uint8_t devAddr = 0;
117642103316Sopenharmony_ci    uint8_t interfaceId = 0;
117742103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
117842103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
117942103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, interfaceId, UEC_SERVICE_WRITE_PARCEL_ERROR);
118042103316Sopenharmony_ci    bool unactivated;
118142103316Sopenharmony_ci    int32_t ret = GetInterfaceActiveStatus(busNum, devAddr, interfaceId, unactivated);
118242103316Sopenharmony_ci    if (ret == UEC_OK) {
118342103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(reply, Bool, unactivated, UEC_SERVICE_WRITE_PARCEL_ERROR);
118442103316Sopenharmony_ci    } else {
118542103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
118642103316Sopenharmony_ci    }
118742103316Sopenharmony_ci    return ret;
118842103316Sopenharmony_ci}
118942103316Sopenharmony_ci
119042103316Sopenharmony_ciint32_t UsbServerStub::DoGetDeviceSpeed(MessageParcel &data, MessageParcel &reply, MessageOption &option)
119142103316Sopenharmony_ci{
119242103316Sopenharmony_ci    uint8_t busNum = 0;
119342103316Sopenharmony_ci    uint8_t devAddr = 0;
119442103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, busNum, UEC_SERVICE_WRITE_PARCEL_ERROR);
119542103316Sopenharmony_ci    READ_PARCEL_WITH_RET(data, Uint8, devAddr, UEC_SERVICE_WRITE_PARCEL_ERROR);
119642103316Sopenharmony_ci    uint8_t speed;
119742103316Sopenharmony_ci    int32_t ret = GetDeviceSpeed(busNum, devAddr, speed);
119842103316Sopenharmony_ci    if (ret == UEC_OK) {
119942103316Sopenharmony_ci        WRITE_PARCEL_WITH_RET(reply, Uint8, speed, UEC_SERVICE_WRITE_PARCEL_ERROR);
120042103316Sopenharmony_ci    } else {
120142103316Sopenharmony_ci        USB_HILOGE(MODULE_USBD, "ret:%{public}d", ret);
120242103316Sopenharmony_ci    }
120342103316Sopenharmony_ci    USB_HILOGE(MODULE_USBD, "DoGetDeviceSpeed speed:%{public}u", speed);
120442103316Sopenharmony_ci    return ret;
120542103316Sopenharmony_ci}
120642103316Sopenharmony_ci
120742103316Sopenharmony_ci} // namespace USB
120842103316Sopenharmony_ci} // namespace OHOS
1209