1 /* 2 * Copyright (C) 2022 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 "bluetooth_avrcp_meItem.h" 17 18 namespace OHOS { 19 namespace Bluetooth { 20 const uint32_t AVRCP_READ_FROM_PARCEL_COUNT_MAX = 0xFFFF; Marshalling(Parcel & parcel) const21bool BluetoothAvrcpMeItem::Marshalling(Parcel &parcel) const 22 { 23 if (!parcel.WriteUint8(itemType_)) { 24 return false; 25 } 26 if (!parcel.WriteUint64(uid_)) { 27 return false; 28 } 29 if (!parcel.WriteUint8(type_)) { 30 return false; 31 } 32 if (!parcel.WriteUint8(playable_)) { 33 return false; 34 } 35 if (!parcel.WriteString(name_)) { 36 return false; 37 } 38 if (!parcel.WriteUint32(attributes_.size())) { 39 return false; 40 } 41 for (auto &attribute : attributes_) { 42 if (!parcel.WriteUint32(attribute)) { 43 return false; 44 } 45 } 46 if (!parcel.WriteUint32(values_.size())) { 47 return false; 48 } 49 for (auto &value : values_) { 50 if (!parcel.WriteString(value)) { 51 return false; 52 } 53 } 54 return true; 55 } 56 WriteToParcel(Parcel & parcel)57bool BluetoothAvrcpMeItem::WriteToParcel(Parcel &parcel) 58 { 59 return Marshalling(parcel); 60 } 61 Unmarshalling(Parcel & parcel)62BluetoothAvrcpMeItem *BluetoothAvrcpMeItem::Unmarshalling(Parcel &parcel) 63 { 64 BluetoothAvrcpMeItem* avrcpData = new BluetoothAvrcpMeItem(); 65 if (avrcpData != nullptr && !avrcpData->ReadFromParcel(parcel)) { 66 delete avrcpData; 67 avrcpData = nullptr; 68 } 69 return avrcpData; 70 } 71 ReadFromParcel(Parcel & parcel)72bool BluetoothAvrcpMeItem::ReadFromParcel(Parcel &parcel) 73 { 74 if (!parcel.ReadUint8(itemType_)) { 75 return false; 76 } 77 if (!parcel.ReadUint64(uid_)) { 78 return false; 79 } 80 if (!parcel.ReadUint8(type_)) { 81 return false; 82 } 83 if (!parcel.ReadUint8(playable_)) { 84 return false; 85 } 86 if (!parcel.ReadString(name_)) { 87 return false; 88 } 89 uint32_t size = 0; 90 if (!parcel.ReadUint32(size) || size > AVRCP_READ_FROM_PARCEL_COUNT_MAX) { 91 return false; 92 } 93 for (size_t i = 0; i < size; i++) { 94 uint32_t attribute = 0; 95 if (!parcel.ReadUint32(attribute)) { 96 return false; 97 } 98 attributes_.push_back(attribute); 99 } 100 if (!parcel.ReadUint32(size) || size > AVRCP_READ_FROM_PARCEL_COUNT_MAX) { 101 return false; 102 } 103 for (size_t i = 0; i < size; i++) { 104 std::string value; 105 if (!parcel.ReadString(value)) { 106 return false; 107 } 108 values_.push_back(value); 109 } 110 return true; 111 } 112 113 } // namespace Bluetooth 114 } // namespace OHOS