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_desc"
17 #endif
18 
19 #include "securec.h"
20 #include "bluetooth_bt_uuid.h"
21 #include "bluetooth_gatt_descriptor_parcel.h"
22 #include "bluetooth_log.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 const uint32_t GATT_DESCRIPTOR_PARCEL_SIZE_MAX = 0x100;
Marshalling(Parcel & parcel) const27 bool BluetoothGattDescriptor::Marshalling(Parcel &parcel) const
28 {
29     if (!parcel.WriteUint16(handle_)) {
30         return false;
31     }
32     if (!parcel.WriteInt32(permissions_)) {
33         return false;
34     }
35     if (!parcel.WriteUint32(length_)) {
36         return false;
37     }
38     for (size_t i = 0; i < length_; i++) {
39         if (!parcel.WriteUint8(value_[i])) {
40             return false;
41         }
42     }
43     BluetoothUuid uuid(uuid_);
44     if (!parcel.WriteParcelable(&uuid)) {
45         return false;
46     }
47     return true;
48 }
49 
Unmarshalling(Parcel & parcel)50 BluetoothGattDescriptor *BluetoothGattDescriptor::Unmarshalling(Parcel &parcel)
51 {
52     BluetoothGattDescriptor *descriptor = new BluetoothGattDescriptor();
53     if (descriptor != nullptr && !descriptor->ReadFromParcel(parcel)) {
54         delete descriptor;
55         descriptor = nullptr;
56     }
57     return descriptor;
58 }
59 
WriteToParcel(Parcel & parcel)60 bool BluetoothGattDescriptor::WriteToParcel(Parcel &parcel)
61 {
62     return Marshalling(parcel);
63 }
64 
ReadFromParcel(Parcel & parcel)65 bool BluetoothGattDescriptor::ReadFromParcel(Parcel &parcel)
66 {
67     if (!parcel.ReadUint16(handle_)) {
68         return false;
69     }
70     if (!parcel.ReadInt32(permissions_)) {
71         return false;
72     }
73     uint32_t length;
74     if (!parcel.ReadUint32(length) || length > GATT_DESCRIPTOR_PARCEL_SIZE_MAX) {
75         HILOGE("read parcel length error, length=0x%{public}x", length);
76         return false;
77     }
78     length_ = length;
79     if (length > 0) {
80         uint8_t value[length_];
81         for (size_t i = 0; i < length_; i++) {
82             if (!parcel.ReadUint8(value[i])) {
83                 return false;
84             }
85         }
86         value_ = std::make_unique<uint8_t[]>(length);
87         if (memcpy_s(value_.get(), length, value, length_) != EOK) {
88             HILOGE("BluetoothGattDescriptor::ReadFromParcel error");
89             return false;
90         }
91     }
92 
93     std::shared_ptr<BluetoothUuid> uuid(parcel.ReadParcelable<BluetoothUuid>());
94     if (!uuid) {
95         return false;
96     }
97     uuid_ = bluetooth::Uuid(*uuid);
98     return true;
99 }
100 }  // namespace Bluetooth
101 }  // namespace OHOS
102