• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "struct_parcel.h"
17 
18 namespace OHOS {
19 namespace USB {
20 
Marshalling(Parcel & out) const21 bool UsbIsoParcel::Marshalling(Parcel &out) const
22 {
23     if (!out.WriteInt32(isoInfo.isoLength)) {
24         return false;
25     }
26     if (!out.WriteInt32(isoInfo.isoActualLength)) {
27         return false;
28     }
29     if (!out.WriteInt32(isoInfo.isoStatus)) {
30         return false;
31     }
32 
33     return true;
34 }
35 
Unmarshalling(Parcel & in)36 UsbIsoParcel *UsbIsoParcel::Unmarshalling(Parcel &in)
37 {
38     auto *usbIsoParcel = new (std::nothrow) UsbIsoParcel();
39     if (usbIsoParcel == nullptr) {
40         return nullptr;
41     }
42 
43     usbIsoParcel->isoInfo.isoLength = in.ReadInt32();
44     usbIsoParcel->isoInfo.isoActualLength = in.ReadInt32();
45     usbIsoParcel->isoInfo.isoStatus = in.ReadInt32();
46     return usbIsoParcel;
47 }
48 
Marshalling(Parcel & out) const49 bool UsbIsoVecParcel::Marshalling(Parcel &out) const
50 {
51     const std::vector<HDI::Usb::V1_2::UsbIsoPacketDescriptor> isoInfoVec = this->isoInfoVec;
52     uint32_t vecSize = isoInfoVec.size();
53     if (!out.WriteUint32(vecSize)) {
54         return false;
55     }
56 
57     for (uint32_t index = 0; index < vecSize; index++) {
58         sptr<UsbIsoParcel> usbIsoParcel = new (std::nothrow) UsbIsoParcel();
59         if (usbIsoParcel == nullptr) {
60             return false;
61         }
62 
63         usbIsoParcel->isoInfo = isoInfoVec.at(index);
64         if (!out.WriteParcelable(usbIsoParcel)) {
65             usbIsoParcel.clear();
66             return false;
67         }
68     }
69     return true;
70 }
71 
Unmarshalling(Parcel & in)72 UsbIsoVecParcel *UsbIsoVecParcel::Unmarshalling(Parcel &in)
73 {
74     UsbIsoVecParcel *usbIsoVecParcel = new (std::nothrow) UsbIsoVecParcel();
75     if (usbIsoVecParcel == nullptr) {
76         return nullptr;
77     }
78 
79     uint32_t vecSize;
80     if (!in.ReadUint32(vecSize)) {
81         delete (usbIsoVecParcel);
82         usbIsoVecParcel = nullptr;
83         return nullptr;
84     }
85 
86     for (uint32_t index = 0; index < vecSize; index++) {
87         sptr<UsbIsoParcel> usbIsoParcel = in.ReadParcelable<UsbIsoParcel>();
88         if (usbIsoParcel == nullptr) {
89             delete (usbIsoVecParcel);
90             usbIsoVecParcel = nullptr;
91             return nullptr;
92         }
93         usbIsoVecParcel->isoInfoVec.emplace_back(usbIsoParcel->isoInfo);
94     }
95     return usbIsoVecParcel;
96 }
97 
98 #ifdef USB_MANAGER_PASS_THROUGH
Marshalling(Parcel & out) const99 bool UsbPassIsoParcel::Marshalling(Parcel &out) const
100 {
101     if (!out.WriteInt32(isoInfo.isoLength)) {
102         return false;
103     }
104     if (!out.WriteInt32(isoInfo.isoActualLength)) {
105         return false;
106     }
107     if (!out.WriteInt32(isoInfo.isoStatus)) {
108         return false;
109     }
110 
111     return true;
112 }
113 
Unmarshalling(Parcel & in)114 UsbPassIsoParcel *UsbPassIsoParcel::Unmarshalling(Parcel &in)
115 {
116     auto *usbPassIsoParcel = new (std::nothrow) UsbPassIsoParcel();
117     if (usbPassIsoParcel == nullptr) {
118         return nullptr;
119     }
120 
121     usbPassIsoParcel->isoInfo.isoLength = in.ReadInt32();
122     usbPassIsoParcel->isoInfo.isoActualLength = in.ReadInt32();
123     usbPassIsoParcel->isoInfo.isoStatus = in.ReadInt32();
124     return usbPassIsoParcel;
125 }
126 
Marshalling(Parcel & out) const127 bool UsbPassIsoVecParcel::Marshalling(Parcel &out) const
128 {
129     const std::vector<HDI::Usb::V2_0::UsbIsoPacketDescriptor> isoInfoVec = this->isoInfoVec;
130     uint32_t vecSize = isoInfoVec.size();
131     if (!out.WriteUint32(vecSize)) {
132         return false;
133     }
134 
135     for (uint32_t index = 0; index < vecSize; index++) {
136         sptr<UsbPassIsoParcel> usbPassIsoParcel = new (std::nothrow) UsbPassIsoParcel();
137         if (usbPassIsoParcel == nullptr) {
138             return false;
139         }
140 
141         usbPassIsoParcel->isoInfo = isoInfoVec.at(index);
142         if (!out.WriteParcelable(usbPassIsoParcel)) {
143             return false;
144         }
145     }
146     return true;
147 }
148 
Unmarshalling(Parcel & in)149 UsbPassIsoVecParcel *UsbPassIsoVecParcel::Unmarshalling(Parcel &in)
150 {
151     UsbPassIsoVecParcel *usbPassIsoVecParcel = new (std::nothrow) UsbPassIsoVecParcel();
152     if (usbPassIsoVecParcel == nullptr) {
153         return nullptr;
154     }
155 
156     uint32_t vecSize;
157     if (!in.ReadUint32(vecSize)) {
158         delete (usbPassIsoVecParcel);
159         usbPassIsoVecParcel = nullptr;
160         return nullptr;
161     }
162 
163     for (uint32_t index = 0; index < vecSize; index++) {
164         sptr<UsbPassIsoParcel> usbPassIsoParcel = in.ReadParcelable<UsbPassIsoParcel>();
165         if (usbPassIsoParcel == nullptr) {
166             delete (usbPassIsoVecParcel);
167             usbPassIsoVecParcel = nullptr;
168             return nullptr;
169         }
170         usbPassIsoVecParcel->isoInfoVec.emplace_back(usbPassIsoParcel->isoInfo);
171     }
172     return usbPassIsoVecParcel;
173 }
174 #endif // USB_MANAGER_PASS_THROUGH
175 } // namespace USB
176 } // namespace OHOS
177