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