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_a2dp_a2dpCodecStatus.h"
17 #include <memory>
18 #include <vector>
19 #include "bluetooth_log.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
23 const uint32_t A2DP_A2DPCODECSTATUS_PARCEL_SIZE_MAX = 0x400;
Marshalling(Parcel & parcel) const24 bool BluetoothA2dpCodecStatus::Marshalling(Parcel &parcel) const
25 {
26     BluetoothA2dpCodecInfo info = bluetooth::CodecInfo(codecInfo);
27     if (!parcel.WriteParcelable(&info)) {
28         return false;
29     }
30     if (!parcel.WriteUint32(codecInfoLocalCap.size())) {
31         return false;
32     }
33     for (auto &infoLocalCap : codecInfoLocalCap) {
34         info = bluetooth::CodecInfo(infoLocalCap);
35         if (!parcel.WriteParcelable(&info)) {
36             return false;
37         }
38     }
39     if (!parcel.WriteUint32(codecInfoConfirmCap.size())) {
40         return false;
41     }
42     for (auto &infoConfirmCap : codecInfoConfirmCap) {
43         info = bluetooth::CodecInfo(infoConfirmCap);
44         if (!parcel.WriteParcelable(&info)) {
45             return false;
46         }
47     }
48     return true;
49 }
50 
WriteToParcel(Parcel & parcel)51 bool BluetoothA2dpCodecStatus::WriteToParcel(Parcel &parcel)
52 {
53     return Marshalling(parcel);
54 }
55 
Unmarshalling(Parcel & parcel)56 BluetoothA2dpCodecStatus *BluetoothA2dpCodecStatus::Unmarshalling(Parcel &parcel)
57 {
58     BluetoothA2dpCodecStatus *codecData = new BluetoothA2dpCodecStatus();
59     if (codecData != nullptr && !codecData->ReadFromParcel(parcel)) {
60         delete codecData;
61         codecData = nullptr;
62     }
63     return codecData;
64 }
65 
ReadFromParcel(Parcel & parcel)66 bool BluetoothA2dpCodecStatus::ReadFromParcel(Parcel &parcel)
67 {
68     std::shared_ptr<BluetoothA2dpCodecInfo> info(parcel.ReadParcelable<BluetoothA2dpCodecInfo>());
69     if (!info) {
70         return false;
71     }
72     codecInfo = bluetooth::CodecInfo(*info);
73     uint32_t size = 0;
74     if (!parcel.ReadUint32(size) || size > A2DP_A2DPCODECSTATUS_PARCEL_SIZE_MAX) {
75         return false;
76     }
77     for (size_t i = 0; i < size; i++) {
78         std::shared_ptr<BluetoothA2dpCodecInfo> infoLocalCap(parcel.ReadParcelable<BluetoothA2dpCodecInfo>());
79         if (!infoLocalCap) {
80             return false;
81         }
82         codecInfoLocalCap.push_back(*infoLocalCap);
83     }
84     if (!parcel.ReadUint32(size) || size > A2DP_A2DPCODECSTATUS_PARCEL_SIZE_MAX) {
85         return false;
86     }
87     for (size_t i = 0; i < size; i++) {
88         std::shared_ptr<BluetoothA2dpCodecInfo> infoConfirmCap(parcel.ReadParcelable<BluetoothA2dpCodecInfo>());
89         if (!infoConfirmCap) {
90             return false;
91         }
92         codecInfoConfirmCap.push_back(*infoConfirmCap);
93     }
94     return true;
95 }
96 
Marshalling(Parcel & parcel) const97 bool BluetoothA2dpOffloadCodecStatus::Marshalling(Parcel &parcel) const
98 {
99     BluetoothA2dpOffloadCodecInfo info = BluetoothA2dpOffloadCodecInfo(bluetooth::OffloadCodecInfo(offloadInfo));
100     CHECK_AND_RETURN_LOG_RET(parcel.WriteParcelable(&info), false, "write parcel err.");
101     return true;
102 }
103 
WriteToParcel(Parcel & parcel)104 bool BluetoothA2dpOffloadCodecStatus::WriteToParcel(Parcel &parcel)
105 {
106     return Marshalling(parcel);
107 }
108 
Unmarshalling(Parcel & parcel)109 BluetoothA2dpOffloadCodecStatus *BluetoothA2dpOffloadCodecStatus::Unmarshalling(Parcel &parcel)
110 {
111     BluetoothA2dpOffloadCodecStatus *offloadCodecData = new BluetoothA2dpOffloadCodecStatus();
112     if (offloadCodecData != nullptr && !offloadCodecData->ReadFromParcel(parcel)) {
113         delete offloadCodecData;
114         offloadCodecData = nullptr;
115     }
116     return offloadCodecData;
117 }
118 
ReadFromParcel(Parcel & parcel)119 bool BluetoothA2dpOffloadCodecStatus::ReadFromParcel(Parcel &parcel)
120 {
121     std::shared_ptr<BluetoothA2dpOffloadCodecInfo> info(parcel.ReadParcelable<BluetoothA2dpOffloadCodecInfo>());
122     CHECK_AND_RETURN_LOG_RET(info, false, "Read Parcel err.");
123     offloadInfo = bluetooth::OffloadCodecInfo(*info);
124     return true;
125 }
126 }  // namespace Bluetooth
127 }  // namespace OHOS
128