1 /*
2 * Copyright (c) 2025 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 "usbd_transfer_callback_impl.h"
17 #include "usbd_callback_stub.h"
18 #include "message_option.h"
19 #include "message_parcel.h"
20 #include "usb_errors.h"
21 #include "hilog_wrapper.h"
22 #include "struct_parcel.h"
23
24 namespace OHOS {
25 namespace USB {
OnTransferWriteCallback(int32_t status,int32_t actLength,const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> & isoInfo,const uint64_t userData)26 int32_t UsbdTransferCallbackImpl::OnTransferWriteCallback(int32_t status, int32_t actLength,
27 const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &isoInfo, const uint64_t userData)
28 {
29 if (remote_ == nullptr) {
30 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: remote_ is nullptr", __func__);
31 return UEC_SERVICE_INVALID_VALUE;
32 }
33 OHOS::MessageParcel data;
34 OHOS::MessageParcel reply;
35 OHOS::MessageOption option;
36
37 UsbIsoVecParcel usbIsoVecParcel;
38 usbIsoVecParcel.isoInfoVec = isoInfo;
39 if (!data.WriteParcelable(&usbIsoVecParcel)) {
40 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:write usbIsoVecParcel failed", __func__);
41 return UEC_SERVICE_INVALID_VALUE;
42 }
43 if (!data.WriteInt32(status)) {
44 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: write status failed", __func__);
45 return UEC_SERVICE_INVALID_VALUE;
46 }
47 if (!data.WriteInt32(actLength)) {
48 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: write actLength failed", __func__);
49 return UEC_SERVICE_INVALID_VALUE;
50 }
51 if (!data.WriteUint64(userData)) {
52 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: write userData failed", __func__);
53 return UEC_SERVICE_INVALID_VALUE;
54 }
55
56 int32_t ret = remote_->SendRequest(UsbdStubCallBack::CMD_USBD_TRANSFER_CALLBACK_WRITE, data, reply, option);
57 if (ret != UEC_OK) {
58 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s UsbdStubCallBack failed, error code is %{public}d", __func__, ret);
59 return ret;
60 }
61 return UEC_OK;
62 }
63
OnTransferReadCallback(int32_t status,int32_t actLength,const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> & isoInfo,const uint64_t userData)64 int32_t UsbdTransferCallbackImpl::OnTransferReadCallback(int32_t status, int32_t actLength,
65 const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> &isoInfo, const uint64_t userData)
66 {
67 USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: UsbdTransferCallbackImpl OnTransferReadCallback enter", __func__);
68 if (remote_ == nullptr) {
69 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: remote_ is nullptr", __func__);
70 return UEC_SERVICE_INVALID_VALUE;
71 }
72 OHOS::MessageParcel data;
73 UsbIsoVecParcel usbIsoVecParcel;
74 usbIsoVecParcel.isoInfoVec = isoInfo;
75 if (!data.WriteParcelable(&usbIsoVecParcel)) {
76 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:write usbIsoVecParcel failed", __func__);
77 return UEC_SERVICE_INVALID_VALUE;
78 }
79 if (!data.WriteInt32(status)) {
80 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: write status failed", __func__);
81 return UEC_SERVICE_INVALID_VALUE;
82 }
83 if (!data.WriteInt32(actLength)) {
84 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: write actLength failed", __func__);
85 return UEC_SERVICE_INVALID_VALUE;
86 }
87 if (!data.WriteUint64(userData)) {
88 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: write userData failed", __func__);
89 return UEC_SERVICE_INVALID_VALUE;
90 }
91 OHOS::MessageParcel reply;
92 OHOS::MessageOption option;
93 int32_t ret = remote_->SendRequest(UsbdStubCallBack::CMD_USBD_TRANSFER_CALLBACK_READ, data, reply, option);
94 if (ret != UEC_OK) {
95 USB_HILOGE(MODULE_USB_SERVICE, "%{public}s UsbdStubCallBack failed, error code is %{public}d", __func__, ret);
96 return ret;
97 }
98 return UEC_OK;
99 }
100 } // namespace USB
101 } // namespace OHOS
102