• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 "raw_address.h"
25 
26 namespace bluetooth {
27 struct Descriptor {
DescriptorDescriptor28     Descriptor() : handle_(0), permissions_(0), value_(nullptr), length_(0), uuid_() {}
29 
DescriptorDescriptor30     Descriptor(const Uuid& uuid, const int permissions)
31         : handle_(0), permissions_(permissions), value_(nullptr), length_(0), uuid_(uuid)
32     {
33     }
34 
DescriptorDescriptor35     Descriptor(uint16_t handle, const Uuid& uuid, int permissions)
36         : handle_(handle), permissions_(permissions), value_(nullptr), length_(0), uuid_(uuid)
37     {
38     }
39 
40     Descriptor(const Uuid &uuid, uint16_t handle, int permissions, const uint8_t *value, size_t length);
41 
42     Descriptor(const Descriptor &src);
43 
DescriptorDescriptor44     Descriptor(Descriptor&& src)
45         : handle_(src.handle_), permissions_(src.permissions_), value_(std::move(src.value_)), length_(src.length_),
46         uuid_(src.uuid_)
47     {
48         src.value_ = nullptr;
49         src.length_ = 0;
50     }
51 
DescriptorDescriptor52     explicit Descriptor(uint16_t handle) : handle_(handle), permissions_(0), value_(nullptr), length_(0), uuid_() {}
53 
54     Descriptor(uint16_t handle, const uint8_t *value, size_t length);
55 
~DescriptorDescriptor56     ~Descriptor(){}
57 
58     Descriptor &operator=(const Descriptor &src) = delete;
59     Descriptor &operator=(Descriptor &&src) = delete;
60 
61     uint16_t handle_;
62     int permissions_;
63     std::unique_ptr<uint8_t[]> value_;
64     size_t length_;
65     Uuid uuid_;
66 };
67 
68 struct Characteristic {
CharacteristicCharacteristic69     Characteristic()
70         : handle_(0), endHandle_(0), valueHandle_(0), properties_(0),
71           permissions_(0), value_(nullptr), length_(0), uuid_(), descriptors_() {}
72 
CharacteristicCharacteristic73     Characteristic(const Uuid& uuid, uint16_t handle, int properties)
74         : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(properties), permissions_(0),
75         value_(nullptr),  length_(0), uuid_(uuid), descriptors_()
76     {
77     }
78 
79     Characteristic(
80         const Uuid &uuid, uint16_t handle, int properties, int permissions, const uint8_t *value, size_t length);
81 
82     Characteristic(uint16_t handle, const uint8_t *value, size_t length);
83 
84     void SetValue(const uint8_t *value, size_t length);
85 
CharacteristicCharacteristic86     explicit Characteristic(uint16_t handle)
87         : handle_(handle), endHandle_(0), valueHandle_(handle + 1), properties_(0), permissions_(0),
88         value_(nullptr), length_(0), uuid_(), descriptors_() {}
89 
CharacteristicCharacteristic90     Characteristic(uint16_t handle, uint16_t endHandle)
91         : handle_(handle), endHandle_(endHandle), valueHandle_(handle + 1), properties_(0), permissions_(0),
92         value_(nullptr), length_(0), uuid_(), descriptors_()
93     {
94     }
95 
~CharacteristicCharacteristic96     ~Characteristic(){}
97 
98     Characteristic(const Characteristic &src);
99 
CharacteristicCharacteristic100     Characteristic(Characteristic&& src)
101         : handle_(src.handle_), endHandle_(src.endHandle_), valueHandle_(src.handle_ + 1),
102         properties_(src.properties_), permissions_(src.permissions_), value_(std::move(src.value_)),
103         length_(src.length_), uuid_(src.uuid_), descriptors_(std::move(src.descriptors_))
104     {
105         src.value_ = nullptr;
106         src.length_ = 0;
107     }
108 
109     Characteristic &operator=(const Characteristic &src) = delete;
110     Characteristic &operator=(Characteristic &&src) = delete;
111 
112     uint16_t handle_;
113     uint16_t endHandle_;
114     uint16_t valueHandle_;
115     int properties_;
116     int permissions_;
117     std::unique_ptr<uint8_t[]> value_;
118     size_t length_;
119     Uuid uuid_;
120     std::vector<Descriptor> descriptors_;
121 };
122 
123 struct Service {
ServiceService124     Service()
125         :isPrimary_(false), handle_(0), startHandle_(0), endHandle_(0), uuid_(), includeServices_(), characteristics_()
126     {
127     }
128 
ServiceService129     Service(const Uuid& uuid, uint16_t handle, uint16_t starthandle, uint16_t endHandle)
130         : isPrimary_(true), handle_(handle), startHandle_(starthandle), endHandle_(endHandle), uuid_(uuid),
131           includeServices_(), characteristics_()
132     {
133     }
134 
ServiceService135     explicit Service(uint16_t handle)
136         : isPrimary_(false), handle_(handle), startHandle_(handle), endHandle_(0), uuid_(), includeServices_(),
137         characteristics_()
138     {
139     }
140 
ServiceService141     Service(uint16_t handle, uint16_t endHandle)
142         : isPrimary_(false), handle_(handle), startHandle_(handle), endHandle_(endHandle), uuid_(), includeServices_(),
143         characteristics_()
144     {
145     }
146 
147     Service(const Service&) = default;
148     Service(Service&&) = default;
149 
150     Service &operator=(const Service &src) = delete;
151     Service &operator=(Service &&src) = delete;
152 
153     bool isPrimary_;
154     uint16_t handle_;
155     uint16_t startHandle_;
156     uint16_t endHandle_;
157     Uuid uuid_;
158     std::vector<Service> includeServices_;
159     std::vector<Characteristic> characteristics_;
160 };
161 
162 struct GattDevice {
GattDeviceGattDevice163     GattDevice() : isEncryption_(false), transport_(0), addressType_(0), connectState_(0), addr_() {}
164     GattDevice(const RawAddress &addr, uint8_t type, uint8_t transport);
165 
166     GattDevice(const RawAddress &addr, uint8_t type, uint8_t transport, int state);
167 
168     GattDevice(const RawAddress &addr, uint8_t transport);
169 
170     bool isEncryption_ = false;
171     uint8_t role_ = GATT_ROLE_INVALID;
172     uint8_t transport_ = 0;
173     uint8_t addressType_ = 0;
174     int connectState_ = 0;
175     RawAddress addr_ = {};
176 
177     bool operator==(const GattDevice& rhs) const
178     {
179         return ((addr_ == rhs.addr_) && (transport_ == rhs.transport_));
180     }
181 
182     bool operator<(const GattDevice& rhs) const
183     {
184         return ((addr_ < rhs.addr_) && (transport_ == rhs.transport_));
185     }
186 };
187 } // namespace bluetooth
188 #endif // GATT_DATA_H
189