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