1 /* 2 * Copyright (C) 2021 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 #ifndef GATT_DATABASE_H 17 #define GATT_DATABASE_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <list> 22 #include <map> 23 #include <memory> 24 #include <set> 25 #include <vector> 26 #include "base_def.h" 27 #include "bt_uuid.h" 28 #include "gatt_data.h" 29 #include "securec.h" 30 31 namespace bluetooth { 32 class GattDatabase { 33 public: 34 struct AttributeValue { 35 std::unique_ptr<uint8_t[]> value_; 36 size_t length_; 37 AttributeValueAttributeValue38 AttributeValue() : value_(nullptr), length_(0) 39 {} 40 AttributeValueAttributeValue41 AttributeValue(uint8_t **value, size_t length) : value_(nullptr), length_(length) 42 { 43 value_.reset(*value); 44 *value = nullptr; 45 } 46 47 AttributeValue(const AttributeValue &src) = delete; 48 49 AttributeValue &operator=(const AttributeValue &src) 50 { 51 if (this != &src) { 52 auto tmp = std::make_unique<uint8_t[]>(src.length_); 53 if (tmp) { 54 length_ = src.length_; 55 value_ = std::move(tmp); 56 (void)memcpy_s(value_.get(), length_, src.value_.get(), length_); 57 } 58 } 59 return *this; 60 } 61 AttributeValueAttributeValue62 AttributeValue(AttributeValue &&src) noexcept : value_(std::move(src.value_)), length_(src.length_) 63 { 64 src.length_ = 0; 65 } 66 67 AttributeValue &operator=(AttributeValue &&src) noexcept 68 { 69 if (this != &src) { 70 value_ = std::move(src.value_); 71 length_ = src.length_; 72 src.length_ = 0; 73 } 74 return *this; 75 } 76 SetValueAttributeValue77 bool SetValue(const uint8_t *value, size_t length) 78 { 79 length_ = length; 80 value_ = std::make_unique<uint8_t[]>(length_); 81 if (memcpy_s(value_.get(), length_, value, length_) != EOK) { 82 return false; 83 } 84 return true; 85 } 86 ~AttributeValueAttributeValue87 ~AttributeValue() 88 {} 89 }; 90 91 struct AttributeEntity { 92 uint8_t permissions_; 93 uint16_t handle_; 94 AttributeValue value_; 95 Uuid type_; AttributeEntityAttributeEntity96 AttributeEntity(uint8_t permissions, uint16_t handle, const Uuid &uuid) 97 : permissions_(permissions), handle_(handle), value_(), type_(uuid) 98 {} 99 SetValueAttributeEntity100 bool SetValue(const uint8_t *value, size_t length) 101 { 102 return value_.SetValue(value, length); 103 } 104 105 AttributeEntity(const AttributeEntity &) = delete; 106 AttributeEntity &operator=(const AttributeEntity &) = default; 107 AttributeEntity(AttributeEntity &&) = default; 108 AttributeEntity &operator=(AttributeEntity &&) = default; 109 }; 110 111 struct Descriptor { 112 uint8_t permissions_; 113 uint16_t handle_; 114 AttributeValue value_; 115 Uuid uuid_; DescriptorDescriptor116 explicit Descriptor(const bluetooth::Descriptor &src) 117 : permissions_(src.permissions_), handle_(src.handle_), value_(), uuid_(src.uuid_) 118 {} 119 120 Descriptor(const Descriptor &) = delete; 121 Descriptor &operator=(const Descriptor &) = default; 122 Descriptor(Descriptor &&) = default; 123 Descriptor &operator=(Descriptor &&) = default; 124 }; 125 126 struct Characteristic { 127 uint8_t properties_; 128 uint16_t handle_; 129 uint16_t valueHandle_; 130 uint16_t endHandle_; 131 std::map<uint16_t, Descriptor> descriptors_; 132 Uuid uuid_; 133 CharacteristicCharacteristic134 explicit Characteristic(const bluetooth::Characteristic &src) 135 : properties_(src.properties_), 136 handle_(src.handle_), 137 valueHandle_(src.valueHandle_), 138 endHandle_(), 139 descriptors_(), 140 uuid_(src.uuid_) 141 {} 142 143 Characteristic(const Characteristic &) = delete; 144 Characteristic &operator=(const Characteristic &) = default; 145 Characteristic(Characteristic &&) = default; 146 Characteristic &operator=(Characteristic &&) = default; 147 }; 148 149 struct IncludeService { 150 uint16_t handle_; 151 uint16_t startHandle_; 152 uint16_t endHandle_; 153 Uuid uuid_; 154 IncludeServiceIncludeService155 explicit IncludeService(const bluetooth::Service &src) 156 : handle_(src.handle_), startHandle_(src.startHandle_), endHandle_(src.endHandle_), uuid_(src.uuid_) 157 {} 158 }; 159 160 struct Service { 161 bool isPrimary_; 162 uint16_t handle_; 163 uint16_t endHandle_; 164 std::vector<IncludeService> includeServices_; 165 std::map<uint16_t, Characteristic> characteristics_; 166 Uuid uuid_; 167 ServiceService168 Service(uint16_t handle, uint16_t endHandle, const Uuid uuid) 169 : isPrimary_(false), 170 handle_(handle), 171 endHandle_(endHandle), 172 includeServices_(), 173 characteristics_(), 174 uuid_(uuid) 175 {} 176 ServiceService177 explicit Service(const bluetooth::Service &src) 178 : isPrimary_(src.isPrimary_), 179 handle_(src.handle_), 180 endHandle_(src.endHandle_), 181 includeServices_(), 182 characteristics_(), 183 uuid_(src.uuid_) 184 {} 185 186 Service(const Service &) = delete; 187 Service &operator=(const Service &) = default; 188 Service(Service &&) = default; 189 Service &operator=(Service &&) = default; 190 }; 191 192 using GattAttributeEntity = std::optional<std::reference_wrapper<GattDatabase::AttributeEntity>>; 193 194 GattDatabase(); ~GattDatabase()195 ~GattDatabase() 196 {} 197 int AddService(bluetooth::Service &service); 198 int DeleteService(uint16_t handle); 199 void RemoveAllServices(); 200 int SetValueByHandle(uint16_t handle, AttributeValue &value); 201 const std::map<uint16_t, Service> &GetServices() const; 202 const std::vector<IncludeService> *GetIncludeServices(uint16_t serviceHandle); 203 const GattDatabase::IncludeService *GetIncludeService(uint16_t serviceHandle); 204 const std::map<uint16_t, Characteristic> *GetCharacteristics(uint16_t serviceHandle); 205 const std::map<uint16_t, Descriptor> *GetDescriptors(uint16_t cccHandle); 206 const GattDatabase::Service *GetService(uint16_t handle); 207 GattDatabase::Characteristic *GetCharacteristic(uint16_t valueHandle); 208 const GattDatabase::Descriptor *GetDescriptor(uint16_t valueHandle); 209 GattAttributeEntity GetValueByHandle(const uint16_t handle); 210 int CheckLegalityOfServiceDefinition(bluetooth::Service &service); 211 212 private: 213 static int CountDescriptorByUuid(const std::vector<bluetooth::Descriptor> &descriptors, const Uuid &uuid); 214 std::pair<uint16_t, uint16_t> CalculateAndAssignHandle(const bluetooth::Service &service); 215 void ReleaseHandle(Service &service); 216 bool IsReferenced(uint16_t handle) const; 217 int CheckIncludeServicesLegality(bluetooth::Service &service) const; 218 int CheckCharacteristicsLegality(const bluetooth::Service &service) const; 219 int CheckDescriptorsLegality(const bluetooth::Characteristic &characteristic) const; 220 bool CheckRestrictedGattBasedService(const bluetooth::Service &service); 221 222 // available handle [handle, handle] 223 std::list<std::pair<uint16_t, uint16_t>> availableHandles_ = {}; 224 // service handle <-> Service entity 225 std::map<uint16_t, Service> services_ = {}; 226 // value handle <-> Attribute entity 227 std::map<uint16_t, AttributeEntity> attributes_ = {}; 228 // value handle <-> (service handle, parent handle) 229 // if value handle belong to descriptor, parent handle is characteristic handle witch descriptor belong to. 230 // else parent handle is characteristic handle. 231 std::map<uint16_t, std::pair<uint16_t, uint16_t>> valueHandleMap_ = {}; 232 std::set<uint16_t> restrictedGattBasedService_ = {}; 233 234 DISALLOW_COPY_AND_ASSIGN(GattDatabase); 235 DISALLOW_MOVE_AND_ASSIGN(GattDatabase); 236 }; 237 } // namespace bluetooth 238 239 #endif // !GATT_DATABASE_H 240