1 /*
2 * Copyright (C) Huawei Device Co., Ltd. 2025-2025. All rights reserved.
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_log.h"
17 #include "bluetooth_trust_pair_device.h"
18 #include <iostream>
19 #include <sstream>
20 #include <vector>
21
22 namespace OHOS {
23 namespace Bluetooth {
Marshalling(Parcel & parcel) const24 bool BluetoothTrustPairDevice::Marshalling(Parcel &parcel) const
25 {
26 if (!parcel.WriteString(macAddress_)) {
27 return false;
28 }
29 if (!parcel.WriteString(deviceName_)) {
30 return false;
31 }
32 if (!MarshallingVecStrSafe(parcel, uuids_)) {
33 return false;
34 }
35 if (!parcel.WriteInt32(bluetoothClass_)) {
36 return false;
37 }
38 if (!MarshallingVecSafe(parcel, token_)) {
39 return false;
40 }
41 if (!MarshallingVecSafe(parcel, secureAdvertisingInfo_)) {
42 return false;
43 }
44 return true;
45 }
46
Unmarshalling(Parcel & parcel)47 BluetoothTrustPairDevice *BluetoothTrustPairDevice::Unmarshalling(Parcel &parcel)
48 {
49 BluetoothTrustPairDevice *trustDevice = new BluetoothTrustPairDevice();
50 if (trustDevice != nullptr && !trustDevice->ReadFromParcel(parcel)) {
51 delete trustDevice;
52 trustDevice = nullptr;
53 }
54 return trustDevice;
55 }
56
WriteToParcel(Parcel & parcel)57 bool BluetoothTrustPairDevice::WriteToParcel(Parcel &parcel)
58 {
59 return Marshalling(parcel);
60 }
61
ReadFromParcel(Parcel & parcel)62 bool BluetoothTrustPairDevice::ReadFromParcel(Parcel &parcel)
63 {
64 if (!parcel.ReadString(macAddress_)) {
65 return false;
66 }
67 if (!parcel.ReadString(deviceName_)) {
68 return false;
69 }
70 if (!ReadParcelVecStrSafe(parcel, uuids_)) {
71 return false;
72 }
73 if (!parcel.ReadInt32(bluetoothClass_)) {
74 return false;
75 }
76 if (!ReadParcelVecSafe(parcel, token_)) {
77 return false;
78 }
79 if (!ReadParcelVecSafe(parcel, secureAdvertisingInfo_)) {
80 return false;
81 }
82 return true;
83 }
84
MarshallingVecSafe(Parcel & parcel,const std::vector<uint8_t> & vec) const85 bool BluetoothTrustPairDevice::MarshallingVecSafe(Parcel &parcel, const std::vector<uint8_t> &vec) const
86 {
87 if (vec.size() > TRUST_PAIR_DEVICE_SIZE_MAX) {
88 return false;
89 }
90 if (!parcel.WriteInt32(vec.size())) {
91 return false;
92 }
93 for (auto &data : vec) {
94 if (!parcel.WriteUint8(data)) {
95 return false;
96 }
97 }
98 return true;
99 }
100
ReadParcelVecSafe(Parcel & parcel,std::vector<uint8_t> & res)101 bool BluetoothTrustPairDevice::ReadParcelVecSafe(Parcel &parcel, std::vector<uint8_t> &res)
102 {
103 int32_t vecSize = 0;
104 if (!parcel.ReadInt32(vecSize) || vecSize > TRUST_PAIR_DEVICE_SIZE_MAX) {
105 return false;
106 }
107 uint8_t data = 0;
108 for (auto i = 0; i < vecSize; i++) {
109 if (!parcel.ReadUint8(data)) {
110 return false;
111 }
112 res.push_back(data);
113 }
114 return true;
115 }
116
MarshallingVecStrSafe(Parcel & parcel,const std::vector<std::string> & vec) const117 bool BluetoothTrustPairDevice::MarshallingVecStrSafe(Parcel &parcel, const std::vector<std::string> &vec) const
118 {
119 if (vec.size() > TRUST_PAIR_DEVICE_SIZE_MAX) {
120 return false;
121 }
122 if (!parcel.WriteInt32(vec.size())) {
123 return false;
124 }
125 for (auto &data : vec) {
126 if (!parcel.WriteString(data)) {
127 return false;
128 }
129 }
130 return true;
131 }
132
ReadParcelVecStrSafe(Parcel & parcel,std::vector<std::string> & res)133 bool BluetoothTrustPairDevice::ReadParcelVecStrSafe(Parcel &parcel, std::vector<std::string> &res)
134 {
135 int32_t vecSize = 0;
136 if (!parcel.ReadInt32(vecSize) || vecSize > TRUST_PAIR_DEVICE_SIZE_MAX) {
137 return false;
138 }
139 std::string data;
140 for (auto i = 0; i < vecSize; i++) {
141 if (!parcel.ReadString(data)) {
142 return false;
143 }
144 res.push_back(data);
145 }
146 return true;
147 }
148 } // namespace Bluetooth
149 } // namespace OHOS
150