• 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 "bluetooth_bt_uuid.h"
17 #include "bluetooth_gatt_characteristic_parcel.h"
18 #include "bluetooth_gatt_service_parcel.h"
19 #include "bluetooth_log.h"
20 #include "parcel_bt_uuid.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
24 const uint32_t GATT_SERVICE_PARCEL_SIZE_MAX = 0x100;
Marshalling(Parcel & parcel) const25 bool BluetoothGattService::Marshalling(Parcel &parcel) const
26 {
27     if (!parcel.WriteBool(isPrimary_)) {
28         return false;
29     }
30     if (!parcel.WriteUint16(handle_)) {
31         return false;
32     }
33     if (!parcel.WriteUint16(startHandle_)) {
34         return false;
35     }
36     if (!parcel.WriteUint16(endHandle_)) {
37         return false;
38     }
39     BluetoothUuid uuid(uuid_);
40     if (!parcel.WriteParcelable(&uuid)) {
41         return false;
42     }
43     uint32_t size = includeServices_.size();
44     if (!parcel.WriteUint32(size)) {
45         return false;
46     }
47     for (auto serv : includeServices_) {
48         BluetoothGattService service = BluetoothGattService(serv);
49         if (!parcel.WriteParcelable(&service)) {
50             return false;
51         }
52     }
53     size = characteristics_.size();
54     if (!parcel.WriteUint32(size)) {
55         return false;
56     }
57     for (auto character: characteristics_) {
58         BluetoothGattCharacteristic characteristic = BluetoothGattCharacteristic(character);
59         if (!parcel.WriteParcelable(&characteristic)) {
60             return false;
61         }
62     }
63     return true;
64 }
65 
Unmarshalling(Parcel & parcel)66 BluetoothGattService *BluetoothGattService::Unmarshalling(Parcel &parcel)
67 {
68     BluetoothGattService *service = new BluetoothGattService();
69     if (service != nullptr && !service->ReadFromParcel(parcel)) {
70         delete service;
71         service = nullptr;
72     }
73     return service;
74 }
75 
WriteToParcel(Parcel & parcel)76 bool BluetoothGattService::WriteToParcel(Parcel &parcel)
77 {
78     return Marshalling(parcel);
79 }
80 
ReadFromParcel(Parcel & parcel)81 bool BluetoothGattService::ReadFromParcel(Parcel &parcel)
82 {
83     if (!parcel.ReadBool(isPrimary_)) {
84         return false;
85     }
86     if (!parcel.ReadUint16(handle_)) {
87         return false;
88     }
89     if (!parcel.ReadUint16(startHandle_)) {
90         return false;
91     }
92     if (!parcel.ReadUint16(endHandle_)) {
93         return false;
94     }
95     std::shared_ptr<BluetoothUuid> uuid(parcel.ReadParcelable<BluetoothUuid>());
96     if (!uuid) {
97         return false;
98     }
99     uuid_ = BluetoothUuid(*uuid);
100     uint32_t size = 0;
101     if (!parcel.ReadUint32(size) || size > GATT_SERVICE_PARCEL_SIZE_MAX) {
102         HILOGE("readfailed size value:%{public}u", size);
103         return false;
104     }
105     for (size_t i = 0; i < size; i++) {
106         std::shared_ptr<BluetoothGattService> service(parcel.ReadParcelable<BluetoothGattService>());
107         if (!service) {
108             return false;
109         }
110         includeServices_.push_back(*service);
111     }
112     if (!parcel.ReadUint32(size) || size > GATT_SERVICE_PARCEL_SIZE_MAX) {
113         HILOGE("readfailed size value:%{public}u", size);
114         return false;
115     }
116     for (size_t i = 0; i < size; i++) {
117         std::shared_ptr<BluetoothGattCharacteristic> characteristic(
118             parcel.ReadParcelable<BluetoothGattCharacteristic>());
119         if (!characteristic) {
120             return false;
121         }
122         characteristics_.push_back(*characteristic);
123     }
124     return true;
125 }
126 }  // namespace Bluetooth
127 }  // namespace OHOS
128