1 /* 2 * Copyright 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 #ifndef USB_BULK_RAWDATA_H 17 #define USB_BULK_RAWDATA_H 18 #include <vector> 19 #include "parcel.h" 20 #include "usb_common.h" 21 namespace OHOS { 22 namespace USB { 23 24 class UsbBulkTransData : public Parcelable { 25 public: UsbBulkTransData(std::vector<uint8_t> & buffer)26 UsbBulkTransData(std::vector<uint8_t> &buffer) 27 { 28 data_ = buffer; 29 } 30 UsbBulkTransData()31 UsbBulkTransData() {}; 32 Marshalling(Parcel & parcel)33 bool Marshalling(Parcel &parcel) const override 34 { 35 uint32_t length = data_.size(); 36 const uint8_t *ptr = data_.data(); 37 if (!ptr) { 38 length = 0; 39 } 40 41 if (!parcel.WriteUint32(length)) { 42 USB_HILOGE(MODULE_USBD, "write bulktransfer length failed:%{public}u", length); 43 return false; 44 } 45 if ((ptr) && (length > 0) && !parcel.WriteBuffer(reinterpret_cast<const void *>(ptr), length)) { 46 USB_HILOGE(MODULE_USBD, "write bulktransfer buffer failed length:%{public}u", length); 47 return false; 48 } 49 50 USB_HILOGI(MODULE_USBD, "success bulktransfer length:%{public}u", length); 51 return true; 52 } 53 Unmarshalling(Parcel & parcel)54 static UsbBulkTransData *Unmarshalling(Parcel &parcel) 55 { 56 UsbBulkTransData *pBufferData = new (std::nothrow) UsbBulkTransData; 57 if (pBufferData == nullptr) { 58 return nullptr; 59 } 60 61 uint32_t dataSize = 0; 62 if (!parcel.ReadUint32(dataSize)) { 63 USB_HILOGE(MODULE_USBD, "read bulktransfer dataSize failed"); 64 return pBufferData; 65 } 66 if (dataSize == 0) { 67 USB_HILOGI(MODULE_USBD, "bulktransfer invalid size:%{public}u", dataSize); 68 return pBufferData; 69 } 70 const uint8_t *readData = parcel.ReadUnpadBuffer(dataSize); 71 if (readData == nullptr) { 72 USB_HILOGE(MODULE_USBD, "bulktransfer failed size:%{public}u", dataSize); 73 return pBufferData; 74 } 75 std::vector<uint8_t> tdata(readData, readData + dataSize); 76 pBufferData->data_.swap(tdata); 77 return pBufferData; 78 } 79 80 std::vector<uint8_t> data_; 81 }; 82 83 } // namespace USB 84 } // namespace OHOS 85 86 #endif // USB_BULK_RAWDATA_H