1 /*
2  * Copyright (C) 2021-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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_parcel_gatt_character"
17 #endif
18 
19 #include "securec.h"
20 #include "bluetooth_bt_uuid.h"
21 #include "bluetooth_gatt_characteristic_parcel.h"
22 #include "bluetooth_gatt_descriptor_parcel.h"
23 #include "bluetooth_log.h"
24 
25 namespace OHOS {
26 namespace Bluetooth {
27 const uint32_t GATT_CHARACTERISTIC_PARCEL_SIZE_MAX = 0x400;
Marshalling(Parcel & parcel) const28 bool BluetoothGattCharacteristic::Marshalling(Parcel &parcel) const
29 {
30     if (!parcel.WriteUint16(handle_)) {
31         return false;
32     }
33     if (!parcel.WriteUint16(endHandle_)) {
34         return false;
35     }
36     if (!parcel.WriteUint16(valueHandle_)) {
37         return false;
38     }
39     if (!parcel.WriteInt32(properties_)) {
40         return false;
41     }
42     if (!parcel.WriteInt32(permissions_)) {
43         return false;
44     }
45     if (!parcel.WriteUint32(length_)) {
46         return false;
47     }
48     for (size_t i = 0; i < length_; i++) {
49         if (!parcel.WriteUint8(value_[i])) {
50             return false;
51         }
52     }
53     BluetoothUuid uuid(uuid_);
54     if (!parcel.WriteParcelable(&uuid)) {
55         return false;
56     }
57     uint32_t size = descriptors_.size();
58     if (!parcel.WriteUint32(size)) {
59         return false;
60     }
61     for (auto &des : descriptors_) {
62         BluetoothGattDescriptor descriptor = BluetoothGattDescriptor(des);
63         if (!parcel.WriteParcelable(&descriptor)) {
64             return false;
65         }
66     }
67     return true;
68 }
69 
Unmarshalling(Parcel & parcel)70 BluetoothGattCharacteristic *BluetoothGattCharacteristic::Unmarshalling(Parcel &parcel)
71 {
72     BluetoothGattCharacteristic *characteristic = new BluetoothGattCharacteristic();
73     if (characteristic != nullptr && !characteristic->ReadFromParcel(parcel)) {
74         delete characteristic;
75         characteristic = nullptr;
76     }
77     return characteristic;
78 }
79 
WriteToParcel(Parcel & parcel)80 bool BluetoothGattCharacteristic::WriteToParcel(Parcel &parcel)
81 {
82     return Marshalling(parcel);
83 }
84 
IsReadParcelDataSuccess(Parcel & parcel)85 bool BluetoothGattCharacteristic::IsReadParcelDataSuccess(Parcel &parcel)
86 {
87     if (!parcel.ReadUint16(handle_)) {
88         return false;
89     }
90     if (!parcel.ReadUint16(endHandle_)) {
91         return false;
92     }
93     if (!parcel.ReadUint16(valueHandle_)) {
94         return false;
95     }
96     if (!parcel.ReadInt32(properties_)) {
97         return false;
98     }
99     if (!parcel.ReadInt32(permissions_)) {
100         return false;
101     }
102     return true;
103 }
104 
ReadFromParcel(Parcel & parcel)105 bool BluetoothGattCharacteristic::ReadFromParcel(Parcel &parcel)
106 {
107     if (!IsReadParcelDataSuccess(parcel)) {
108         HILOGE("read parcel data error");
109         return false;
110     }
111     uint32_t length = 0;
112     if (!parcel.ReadUint32(length) || length > GATT_CHARACTERISTIC_PARCEL_SIZE_MAX) {
113         HILOGE("read parcel length error, len=0x%{public}x", length);
114         return false;
115     }
116     length_ = length;
117     if (length > 0) {
118         uint8_t value[length_];
119         for (size_t i = 0; i < length_; i++) {
120             if (!parcel.ReadUint8(value[i])) {
121                 return false;
122             }
123         }
124         value_ = std::make_unique<uint8_t[]>(length);
125         if (memcpy_s(value_.get(), length, value, length_) != EOK) {
126             HILOGE("BluetoothGattCharacteristic::ReadFromParcel error");
127             return false;
128         }
129     }
130     std::shared_ptr<BluetoothUuid> uuid(parcel.ReadParcelable<BluetoothUuid>());
131     if (!uuid) {
132         return false;
133     }
134     uuid_ = bluetooth::Uuid(*uuid);
135     uint32_t size;
136     if (!parcel.ReadUint32(size) || size > GATT_CHARACTERISTIC_PARCEL_SIZE_MAX) {
137         HILOGE("read parcel size error, size=0x%{public}x", size);
138         return false;
139     }
140     for (size_t i = 0; i < size; i++) {
141         std::shared_ptr<BluetoothGattDescriptor> descriptor(parcel.ReadParcelable<BluetoothGattDescriptor>());
142         if (!descriptor) {
143             return false;
144         }
145         descriptors_.push_back(*descriptor);
146     }
147     return true;
148 }
149 }  // namespace Bluetooth
150 }  // namespace OHOS
151