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 #ifndef GATT_DATA_H 17 #define GATT_DATA_H 18 19 #include <cstring> 20 #include <memory> 21 #include <vector> 22 #include "bt_def.h" 23 #include "bt_uuid.h" 24 #include "cstdint" 25 #include "raw_address.h" 26 #include "type_traits" 27 28 29 namespace OHOS { 30 namespace bluetooth { 31 struct Descriptor { DescriptorDescriptor32 Descriptor() : handle_(0), permissions_(0), value_(nullptr), length_(0), uuid_() {} 33 DescriptorDescriptor34 Descriptor(const Uuid& uuid, const int permissions) 35 : handle_(0), permissions_(permissions), value_(nullptr), length_(0), uuid_(uuid) 36 { 37 } 38 DescriptorDescriptor39 Descriptor(uint16_t handle, const Uuid& uuid, int permissions) 40 : handle_(handle), permissions_(permissions), value_(nullptr), length_(0), uuid_(uuid) 41 { 42 } 43 44 Descriptor(const Uuid &uuid, uint16_t handle, int permissions, const uint8_t *value, size_t length); 45 46 Descriptor(const Descriptor &src); 47 DescriptorDescriptor48 Descriptor(Descriptor&& src) 49 : handle_(src.handle_), permissions_(src.permissions_), value_(std::move(src.value_)), length_(src.length_), 50 uuid_(src.uuid_) 51 { 52 src.value_ = nullptr; 53 src.length_ = 0; 54 } 55 DescriptorDescriptor56 explicit Descriptor(uint16_t handle) : handle_(handle), permissions_(0), value_(nullptr), length_(0), uuid_() {} 57 58 Descriptor(uint16_t handle, const uint8_t *value, size_t length); 59 ~DescriptorDescriptor60 ~Descriptor() {} 61 62 Descriptor &operator=(const Descriptor &src) = delete; 63 Descriptor &operator=(Descriptor &&src) = delete; 64 65 uint16_t handle_; 66 int permissions_; 67 std::unique_ptr<uint8_t[]> value_; 68 size_t length_; 69 Uuid uuid_; 70 }; 71 72 struct Characteristic { CharacteristicCharacteristic73 Characteristic() 74 : handle_(0), endHandle_(0), valueHandle_(0), properties_(0), 75 permissions_(0), value_(nullptr), length_(0), uuid_(), descriptors_() {} 76 CharacteristicCharacteristic77 Characteristic(const Uuid& uuid, uint16_t handle, int properties) 78 : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(properties), permissions_(0), 79 value_(nullptr), length_(0), uuid_(uuid), descriptors_() 80 { 81 } 82 83 Characteristic( 84 const Uuid &uuid, uint16_t handle, int properties, int permissions, const uint8_t *value, size_t length); 85 86 Characteristic(uint16_t handle, const uint8_t *value, size_t length); 87 88 void SetValue(const uint8_t *value, size_t length); 89 CharacteristicCharacteristic90 explicit Characteristic(uint16_t handle) 91 : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(0), permissions_(0), 92 value_(nullptr), length_(0), uuid_(), descriptors_() {} 93 CharacteristicCharacteristic94 Characteristic(uint16_t handle, uint16_t endHandle) 95 : handle_(handle), endHandle_(endHandle), valueHandle_(handle + 1), properties_(0), permissions_(0), 96 value_(nullptr), length_(0), uuid_(), descriptors_() 97 { 98 } 99 ~CharacteristicCharacteristic100 ~Characteristic() {} 101 102 Characteristic(const Characteristic &src); 103 CharacteristicCharacteristic104 Characteristic(Characteristic&& src) 105 : handle_(src.handle_), endHandle_(src.endHandle_), valueHandle_(src.handle_ + 1), 106 properties_(src.properties_), permissions_(src.permissions_), value_(std::move(src.value_)), 107 length_(src.length_), uuid_(src.uuid_), descriptors_(std::move(src.descriptors_)) 108 { 109 src.value_ = nullptr; 110 src.length_ = 0; 111 } 112 113 Characteristic &operator=(const Characteristic &src) = delete; 114 Characteristic &operator=(Characteristic &&src) = delete; 115 116 uint16_t handle_; 117 uint16_t endHandle_; 118 uint16_t valueHandle_; 119 int properties_; 120 int permissions_; 121 std::unique_ptr<uint8_t[]> value_; 122 size_t length_; 123 Uuid uuid_; 124 std::vector<Descriptor> descriptors_; 125 }; 126 127 struct Service { ServiceService128 Service() 129 :isPrimary_(false), handle_(0), startHandle_(0), endHandle_(0), uuid_(), includeServices_(), characteristics_() 130 { 131 } 132 ServiceService133 Service(const Uuid& uuid, uint16_t handle, uint16_t starthandle, uint16_t endHandle) 134 : isPrimary_(true), handle_(handle), startHandle_(starthandle), endHandle_(endHandle), uuid_(uuid), 135 includeServices_(), characteristics_() 136 { 137 } 138 ServiceService139 explicit Service(uint16_t handle) 140 : isPrimary_(false), handle_(handle), startHandle_(handle), endHandle_(0), uuid_(), includeServices_(), 141 characteristics_() 142 { 143 } 144 ServiceService145 Service(uint16_t handle, uint16_t endHandle) 146 : isPrimary_(false), handle_(handle), startHandle_(handle), endHandle_(endHandle), uuid_(), includeServices_(), 147 characteristics_() 148 { 149 } 150 151 Service(const Service&) = default; 152 Service(Service&&) = default; 153 154 Service &operator=(const Service &src) = delete; 155 Service &operator=(Service &&src) = delete; 156 157 bool isPrimary_; 158 uint16_t handle_; 159 uint16_t startHandle_; 160 uint16_t endHandle_; 161 Uuid uuid_; 162 std::vector<Service> includeServices_; 163 std::vector<Characteristic> characteristics_; 164 }; 165 166 struct GattDevice { GattDeviceGattDevice167 GattDevice() : isEncryption_(false), transport_(0), addressType_(0), connectState_(0), addr_() {} 168 GattDevice(const RawAddress &addr, uint8_t type, uint8_t transport); 169 170 GattDevice(const RawAddress &addr, uint8_t type, uint8_t transport, int state); 171 172 GattDevice(const RawAddress &addr, uint8_t transport); 173 174 bool isEncryption_ = false; 175 uint8_t role_ = GATT_ROLE_INVALID; 176 uint8_t transport_ = 0; 177 uint8_t addressType_ = 0; 178 int connectState_ = 0; 179 RawAddress addr_ = {}; 180 181 bool operator==(const GattDevice& rhs) const 182 { 183 return ((addr_ == rhs.addr_) && (transport_ == rhs.transport_)); 184 } 185 186 bool operator<(const GattDevice& rhs) const 187 { 188 return ((addr_ < rhs.addr_) && (transport_ == rhs.transport_)); 189 } 190 }; 191 } // namespace bluetooth 192 } // namespace OHOS 193 #endif // GATT_DATA_H 194