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 <cstdint> 20 #include <cstring> 21 #include <memory> 22 #include <vector> 23 #include "bt_def.h" 24 #include "bt_uuid.h" 25 #include "cstdint" 26 #include "raw_address.h" 27 #include "type_traits" 28 29 30 namespace OHOS { 31 namespace bluetooth { 32 struct Descriptor { DescriptorDescriptor33 Descriptor() : handle_(0), permissions_(0), value_(nullptr), length_(0), uuid_() {} 34 DescriptorDescriptor35 Descriptor(const Uuid& uuid, const int permissions) 36 : handle_(0), permissions_(permissions), value_(nullptr), length_(0), uuid_(uuid) 37 { 38 } 39 DescriptorDescriptor40 Descriptor(uint16_t handle, const Uuid& uuid, int permissions) 41 : handle_(handle), permissions_(permissions), value_(nullptr), length_(0), uuid_(uuid) 42 { 43 } 44 45 Descriptor(const Uuid &uuid, uint16_t handle, int permissions, const uint8_t *value, size_t length); 46 47 Descriptor(const Descriptor &src); 48 DescriptorDescriptor49 Descriptor(Descriptor&& src) 50 : handle_(src.handle_), permissions_(src.permissions_), value_(std::move(src.value_)), length_(src.length_), 51 uuid_(src.uuid_) 52 { 53 src.value_ = nullptr; 54 src.length_ = 0; 55 } 56 DescriptorDescriptor57 explicit Descriptor(uint16_t handle) : handle_(handle), permissions_(0), value_(nullptr), length_(0), uuid_() {} 58 59 Descriptor(uint16_t handle, const uint8_t *value, size_t length); 60 ~DescriptorDescriptor61 ~Descriptor(){} 62 63 Descriptor &operator=(const Descriptor &src) = delete; 64 Descriptor &operator=(Descriptor &&src) = delete; 65 66 uint16_t handle_; 67 int permissions_; 68 std::unique_ptr<uint8_t[]> value_; 69 size_t length_; 70 Uuid uuid_; 71 }; 72 73 struct Characteristic { CharacteristicCharacteristic74 Characteristic() 75 : handle_(0), endHandle_(0), valueHandle_(0), properties_(0), 76 permissions_(0), value_(nullptr), length_(0), uuid_(), descriptors_() {} 77 CharacteristicCharacteristic78 Characteristic(const Uuid& uuid, uint16_t handle, int properties) 79 : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(properties), permissions_(0), 80 value_(nullptr), length_(0), uuid_(uuid), descriptors_() 81 { 82 } 83 84 Characteristic( 85 const Uuid &uuid, uint16_t handle, int properties, int permissions, const uint8_t *value, size_t length); 86 87 Characteristic(uint16_t handle, const uint8_t *value, size_t length); 88 89 void SetValue(const uint8_t *value, size_t length); 90 CharacteristicCharacteristic91 explicit Characteristic(uint16_t handle) 92 : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(0), permissions_(0), 93 value_(nullptr), length_(0), uuid_(), descriptors_() {} 94 CharacteristicCharacteristic95 Characteristic(uint16_t handle, uint16_t endHandle) 96 : handle_(handle), endHandle_(endHandle), valueHandle_(handle + 1), properties_(0), permissions_(0), 97 value_(nullptr), length_(0), uuid_(), descriptors_() 98 { 99 } 100 ~CharacteristicCharacteristic101 ~Characteristic(){} 102 103 Characteristic(const Characteristic &src); 104 CharacteristicCharacteristic105 Characteristic(Characteristic&& src) 106 : handle_(src.handle_), endHandle_(src.endHandle_), valueHandle_(src.handle_ + 1), 107 properties_(src.properties_), permissions_(src.permissions_), value_(std::move(src.value_)), 108 length_(src.length_), uuid_(src.uuid_), descriptors_(std::move(src.descriptors_)) 109 { 110 src.value_ = nullptr; 111 src.length_ = 0; 112 } 113 114 Characteristic &operator=(const Characteristic &src) = delete; 115 Characteristic &operator=(Characteristic &&src) = delete; 116 117 uint16_t handle_; 118 uint16_t endHandle_; 119 uint16_t valueHandle_; 120 int properties_; 121 int permissions_; 122 std::unique_ptr<uint8_t[]> value_; 123 size_t length_; 124 Uuid uuid_; 125 std::vector<Descriptor> descriptors_; 126 }; 127 128 struct Service { ServiceService129 Service() 130 :isPrimary_(false), handle_(0), startHandle_(0), endHandle_(0), uuid_(), includeServices_(), characteristics_() 131 { 132 } 133 ServiceService134 Service(const Uuid& uuid, uint16_t handle, uint16_t starthandle, uint16_t endHandle) 135 : isPrimary_(true), handle_(handle), startHandle_(starthandle), endHandle_(endHandle), uuid_(uuid), 136 includeServices_(), characteristics_() 137 { 138 } 139 ServiceService140 explicit Service(uint16_t handle) 141 : isPrimary_(false), handle_(handle), startHandle_(handle), endHandle_(0), uuid_(), includeServices_(), 142 characteristics_() 143 { 144 } 145 ServiceService146 Service(uint16_t handle, uint16_t endHandle) 147 : isPrimary_(false), handle_(handle), startHandle_(handle), endHandle_(endHandle), uuid_(), includeServices_(), 148 characteristics_() 149 { 150 } 151 152 Service(const Service&) = default; 153 Service(Service&&) = default; 154 155 Service &operator=(const Service &src) = delete; 156 Service &operator=(Service &&src) = delete; 157 158 bool isPrimary_; 159 uint16_t handle_; 160 uint16_t startHandle_; 161 uint16_t endHandle_; 162 Uuid uuid_; 163 std::vector<Service> includeServices_; 164 std::vector<Characteristic> characteristics_; 165 }; 166 167 struct GattDevice { GattDeviceGattDevice168 GattDevice() : isEncryption_(false), transport_(0), addressType_(0), connectState_(0), addr_() {} 169 GattDevice(const RawAddress &addr, uint8_t type, uint8_t transport); 170 171 GattDevice(const RawAddress &addr, uint8_t type, uint8_t transport, int state); 172 173 GattDevice(const RawAddress &addr, uint8_t transport); 174 175 bool isEncryption_ = false; 176 uint8_t role_ = GATT_ROLE_INVALID; 177 uint8_t transport_ = 0; 178 uint8_t addressType_ = 0; 179 int connectState_ = 0; 180 RawAddress addr_ = {}; 181 182 bool operator==(const GattDevice& rhs) const 183 { 184 return ((addr_ == rhs.addr_) && (transport_ == rhs.transport_)); 185 } 186 187 bool operator<(const GattDevice& rhs) const 188 { 189 return ((addr_ < rhs.addr_) && (transport_ == rhs.transport_)); 190 } 191 }; 192 } // namespace bluetooth 193 } // namespace OHOS 194 #endif // GATT_DATA_H 195