• 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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief Defines a bluetooth system that provides basic bluetooth connection and profile functions,
21  *        including A2DP, AVRCP, BLE, GATT, HFP, MAP, PBAP, and SPP, etc.
22  *
23  * @since 6
24  *
25  */
26 
27 /**
28  * @file bluetooth_gatt_characteristic.h
29  *
30  * @brief Bluetooth gatt characteristic interface.
31  *
32  * @since 6
33  *
34  */
35 
36 #ifndef BLUETOOTH_GATT_CHARACTERISTIC_H
37 #define BLUETOOTH_GATT_CHARACTERISTIC_H
38 
39 #include <cstddef>
40 #include <cstdint>
41 #include "bluetooth_def.h"
42 #include "bluetooth_gatt_descriptor.h"
43 #include "cstdint"
44 #include <list>
45 #include "memory"
46 #include "uuid.h"
47 #include "vector"
48 
49 namespace OHOS {
50 namespace Bluetooth {
51 /**
52  * @brief Class for GattService functions.
53  *
54  * @since 6
55  *
56  */
57 class GattService;
58 /**
59  * @brief GATT-based Characteristic class
60  * A characteristic is a value used in a service along with properties and configuration information about
61  * how the value is accessed and information about how the value is displayed or represented.
62  */
63 class BLUETOOTH_API GattCharacteristic {
64 public:
65 
66     /** A GATT characteristic value permission.
67      *  Define GATT characteristic permission.
68      */
69     enum Permission {
70         READABLE = 0x01,
71         WRITEABLE = 0x02,
72     };
73 
74 
75     /** A GATT characteristic value write type.
76      *  Define GATT characteristic value write types.
77      */
78     enum WriteType {
79         DEFAULT,
80         NO_RESPONSE,
81         SIGNED,
82     };
83 
84     /** A GATT characteristic propertie.
85      *  Define GATT characteristic properties.
86      */
87     enum Propertie {
88         BROADCAST = 0x01, /**< readable */
89         READ = 0x02,
90         WRITE_WITHOUT_RESPONSE = 0x04,
91         WRITE = 0x08,
92         NOTIFY = 0x10,
93         INDICATE = 0x20,
94         AUTHENTICATED_SIGNED_WRITES = 0x40,
95         EXTENDED_PROPERTIES = 0x80
96     };
97 
98     /**
99      * @brief The function to delete constructor of GattCharacteristic.
100      *
101      * @since 6
102      *
103      */
104     GattCharacteristic() = delete;
105 
106     /**
107      * @brief A constructor of GattCharacteristic.
108      *
109      * @param uuid Uuid of Gatt Characteristic.
110      * @param permissions permissions of Gatt Characteristic.
111      * @param properties properties of Gatt Characteristic.
112      * @since 6
113      *
114      */
115     GattCharacteristic(const UUID uuid, int permissions, int properties);
116 
117     /**
118      * @brief A constructor of GattCharacteristic.
119      *
120      * @param uuid Uuid of Gatt Characteristic.
121      * @param handle handle of Gatt Characteristic.
122      * @param properties properties of Gatt Characteristic.
123      * @param properties properties of Gatt Characteristic.
124      * @since 6
125      *
126      */
127     GattCharacteristic(const UUID uuid, uint16_t handle, const int permissions, const int properties);
128     GattCharacteristic(const GattCharacteristic &);
129     GattCharacteristic &operator=(const GattCharacteristic &);
130     GattCharacteristic(GattCharacteristic &&);
131     GattCharacteristic &operator=(GattCharacteristic &&) = default;
132 
133     /**
134      * @brief The function to add descriptor.
135      *
136      * @param descriptor Descriptor object to add.
137      * @since 6
138      *
139      */
140     void AddDescriptor(const GattDescriptor &descriptor);
141 
142     /**
143      * @brief The function to get descriptor by UUID.
144      *
145      * @param uuid Uuid of Gatt Descriptor.
146      * @return descriptor or nullptr.
147      * @since 6
148      *
149      */
150     GattDescriptor *GetDescriptor(const UUID &uuid);
151 
152     /**
153      * @brief The function to get descriptors.
154      *
155      * @return list of descriptors.
156      * @since 6
157      *
158      */
159     std::vector<GattDescriptor> &GetDescriptors();
160 
161     /**
162      * @brief The function to get handle.
163      *
164      * @return uint16_t  handle.
165      * @since 6
166      *
167      */
168     uint16_t GetHandle() const;
169 
170     /**
171      * @brief The function to get permissions.
172      *
173      * @return permissions.
174      * @since 6
175      *
176      */
177     int GetPermissions() const;
178 
179     /**
180      * @brief The function to get properties.
181      *
182      * @return properties.
183      * @since 6
184      *
185      */
186     int GetProperties() const;
187 
188     /**
189      * @brief The function to get service.
190      *
191      * @return service which characteristic belong to.
192      * @since 6
193      *
194      */
195     GattService *GetService() const;
196 
197     /**
198      * @brief The function to get uuid.
199      *
200      * @return UUID.
201      * @since 6
202      *
203      */
204     const UUID &GetUuid() const;
205 
206     /**
207      * @brief The function to get value.
208      *
209      * @param size size of get value.
210      * @return value pointer.
211      * @since 6
212      *
213      */
214     const std::unique_ptr<uint8_t[]> &GetValue(size_t *size) const;
215 
216     /**
217      * @brief The function to get write type.
218      *
219      * @return write type.
220      * @since 6
221      *
222      */
223     int GetWriteType() const;
224 
225     /**
226      * @brief The function to set write type.
227      *
228      * @param type type of set write type.
229      * @return result of #GATT_STATUS.
230      * @since 6
231      *
232      */
233     int SetWriteType(int type);
234 
235     /**
236      * @brief The function to set value.
237      *
238      * @param values values of set value.
239      * @param length length of set value.
240      * @since 6
241      *
242      */
243     void SetValue(const uint8_t *values, const size_t length);
244 
245 private:
246     /**
247      * @brief The writeType of characteristic.
248      *
249      * @since 6
250      *
251      */
252     uint8_t writeType_;
253 
254     /**
255      * @brief The handle of characteristic.
256      *
257      * @since 6
258      *
259      */
260     uint16_t handle_;
261 
262     /**
263      * @brief The permissions of characteristic.
264      *
265      * @since 6
266      *
267      */
268     int permissions_;
269 
270     /**
271      * @brief The properties of characteristic.
272      *
273      * @since 6
274      *
275      */
276     int properties_;
277 
278     /**
279      * @brief The service of characteristic.
280      *
281      * @since 6
282      *
283      */
284     GattService *service_;
285 
286     /**
287      * @brief The value of characteristic.
288      *
289      * @since 6
290      *
291      */
292     std::unique_ptr<uint8_t[]> value_;
293 
294     /**
295      * @brief The length of characteristic.
296      *
297      * @since 6
298      *
299      */
300     size_t length_;
301 
302     /**
303      * @brief The descriptors of characteristic.
304      *
305      * @since 6
306      *
307      */
308     std::vector<GattDescriptor> descriptors_;
309 
310     /**
311      * @brief The uuid of characteristic.
312      *
313      * @since 6
314      *
315      */
316     UUID uuid_;
317 
318     friend class GattService;
319 };
320 
321 } // namespace Bluetooth
322 } // namespace OHOS
323 
324 #endif  // BLUETOOTH_GATT_CHARACTERISTIC_H
325