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_CACHE_H 17 #define GATT_CACHE_H 18 19 #include <cstdint> 20 #include <map> 21 #include <string> 22 #include <vector> 23 #include "base_def.h" 24 #include "bt_uuid.h" 25 #include "gatt_data.h" 26 27 namespace bluetooth { 28 class GattCache { 29 public: 30 struct IncludeService { 31 uint16_t handle_ = 0; 32 uint16_t startHandle_ = 0; 33 uint16_t endHandle_ = 0; 34 Uuid uuid_ = {}; 35 IncludeServiceIncludeService36 explicit IncludeService(const Service &src) 37 : handle_(src.handle_), startHandle_(src.startHandle_), endHandle_(src.endHandle_), uuid_(src.uuid_) 38 {} IncludeServiceIncludeService39 IncludeService(uint16_t handle, uint16_t startHandle, uint16_t endHandle, const Uuid uuid) 40 : handle_(handle), startHandle_(startHandle), endHandle_(endHandle), uuid_(uuid) 41 {} IncludeServiceIncludeService42 IncludeService(uint16_t handle, uint16_t startHandle, uint16_t endHandle) 43 : handle_(handle), startHandle_(startHandle), endHandle_(endHandle) 44 {} IncludeServiceIncludeService45 explicit IncludeService(const Uuid uuid) : uuid_(uuid) 46 {} 47 }; 48 49 struct Descriptor { 50 uint16_t handle_ = 0; 51 Uuid uuid_ = {}; 52 DescriptorDescriptor53 Descriptor(uint16_t handle, const Uuid uuid) : handle_(handle), uuid_(uuid) 54 {} 55 }; 56 57 struct Characteristic { 58 uint8_t properties_ = 0; 59 uint16_t handle_ = 0; 60 uint16_t valueHandle_ = 0; 61 std::map<uint16_t, Descriptor> descriptors_ = {}; 62 Uuid uuid_ = {}; 63 CharacteristicCharacteristic64 Characteristic(uint16_t handle, uint8_t properties, uint16_t valueHandle, const Uuid uuid) 65 : properties_(properties), handle_(handle), valueHandle_(valueHandle), uuid_(uuid) 66 {} 67 }; 68 69 struct Service { 70 bool isPrimary_ = true; 71 uint16_t handle_ = 0; 72 uint16_t endHandle_ = 0; 73 std::vector<IncludeService> includeServices_ = {}; 74 std::map<uint16_t, Characteristic> characteristics_ = {}; 75 Uuid uuid_ = {}; 76 ServiceService77 Service() 78 {} 79 ServiceService80 Service(bool isPrimary, uint16_t handle, uint16_t endHandle, const Uuid uuid) 81 : isPrimary_(isPrimary), handle_(handle), endHandle_(endHandle), uuid_(uuid) 82 {} 83 }; 84 using Descriptors = std::pair<std::map<uint16_t, GattCache::Descriptor> *, uint16_t>; 85 GattCache()86 GattCache() 87 {} ~GattCache()88 ~GattCache() 89 {} 90 void Clear(); 91 void AddService(const Service &service); 92 int AddIncludeService(uint16_t serviceHandle, const IncludeService &includeService); 93 int AddCharacteristic(uint16_t serviceHandle, const Characteristic &characteristic); 94 int AddDescriptor(uint16_t cccHandle, const Descriptor &descriptor); 95 std::map<uint16_t, Service> &GetServices(); 96 std::vector<IncludeService> *GetIncludeServices(uint16_t serviceHandle); 97 std::map<uint16_t, Characteristic> *GetCharacteristics(uint16_t serviceHandle); 98 Descriptors GetDescriptors(uint16_t cccHandle); 99 const GattCache::Characteristic *GetCharacteristic(int16_t valueHandle); 100 const GattCache::Descriptor *GetDescriptor(int16_t valueHandle); 101 uint16_t GetCharacteristicEndHandle(uint16_t serviceHandle, uint16_t cccHandle) const; 102 103 int StoredToFile(const GattDevice& address) const; 104 int LoadFromFile(const GattDevice& address); 105 106 GattCache(GattCache &&src) = default; 107 GattCache &operator=(GattCache &&src) = default; 108 109 private: 110 struct StorageBlob { 111 uint16_t handle_; 112 Uuid type_; 113 114 union { 115 struct { 116 Uuid uuid_; 117 uint16_t endHandle_; 118 } service_; 119 120 struct { 121 uint16_t handle_; 122 uint16_t endHandle_; 123 Uuid uuid_; 124 } includeService_; 125 126 struct { 127 uint8_t properties_; 128 uint16_t valueHandle_; 129 Uuid uuid_; 130 } characteristic_; 131 } value_; 132 }; 133 134 static const std::string GATT_STORAGE_PRIFIX; 135 136 // service handle <-> struct Service 137 std::map<uint16_t, Service> services_ = {}; 138 // value handle <-> (service handle, parent handle) 139 // if value handle belong to descriptor, parent handle is characteristic handle witch descriptor belong to. 140 // else parent handle is characteristic handle. 141 std::map<uint16_t, std::pair<uint16_t, uint16_t>> valueHandleMap_ = {}; 142 143 static std::string GenerateGattCacheFileName(const GattDevice &address); 144 int WriteStorageBlobToFile(const GattDevice& address, std::vector<StorageBlob> &blob) const; 145 std::vector<StorageBlob> ReadStorageBlobFromFile(const GattDevice &address) const; 146 147 DISALLOW_COPY_AND_ASSIGN(GattCache); 148 }; 149 } // namespace bluetooth 150 151 #endif // !GATT_CACHE_H