• 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     /** A GATT characteristic value write type.
66      *  Define GATT characteristic value write types.
67      */
68     enum WriteType {
69         DEFAULT,
70         NO_RESPONSE,
71         SIGNED,
72     };
73 
74     /** A GATT characteristic propertie.
75      *  Define GATT characteristic properties.
76      */
77     enum Propertie {
78         BROADCAST = 0x01, /**< readable */
79         READ = 0x02,
80         WRITE_WITHOUT_RESPONSE = 0x04,
81         WRITE = 0x08,
82         NOTIFY = 0x10,
83         INDICATE = 0x20,
84         AUTHENTICATED_SIGNED_WRITES = 0x40,
85         EXTENDED_PROPERTIES = 0x80
86     };
87 
88     /** A GATT characteristic permission.
89      *  Define GATT characteristic permission.
90      */
91     enum Permission {
92         READABLE = 0x01,
93         READ_ENCRYPTED = 0x02,
94         READ_ENCRYPTED_MITM = 0x04,
95         WRITEABLE = 0x08,
96         WRITE_ENCRYPTED = 0x10,
97         WRITE_ENCRYPTED_MITM = 0x20,
98         WRITE_SIGNED = 0x40,
99         WRITE_SIGNED_MITM = 0x80
100     };
101 
102     /**
103      * @brief The function to delete constructor of GattCharacteristic.
104      *
105      * @since 6
106      *
107      */
108     GattCharacteristic() = delete;
109 
110     /**
111      * @brief A constructor of GattCharacteristic.
112      *
113      * @param uuid Uuid of Gatt Characteristic.
114      * @param permissions permissions of Gatt Characteristic.
115      * @param properties properties of Gatt Characteristic.
116      * @since 6
117      *
118      */
119     GattCharacteristic(const UUID uuid, int permissions, int properties);
120 
121     /**
122      * @brief A constructor of GattCharacteristic.
123      *
124      * @param uuid Uuid of Gatt Characteristic.
125      * @param handle handle of Gatt Characteristic.
126      * @param properties properties of Gatt Characteristic.
127      * @param properties properties of Gatt Characteristic.
128      * @since 6
129      *
130      */
131     GattCharacteristic(const UUID uuid, uint16_t handle, const int permissions, const int properties);
132     GattCharacteristic(const GattCharacteristic &);
133     GattCharacteristic &operator=(const GattCharacteristic &);
134     GattCharacteristic(GattCharacteristic &&);
135     GattCharacteristic &operator=(GattCharacteristic &&) = default;
136 
137     /**
138      * @brief The function to add descriptor.
139      *
140      * @param descriptor Descriptor object to add.
141      * @since 6
142      *
143      */
144     void AddDescriptor(const GattDescriptor &descriptor);
145 
146     /**
147      * @brief The function to get descriptor by UUID.
148      *
149      * @param uuid Uuid of Gatt Descriptor.
150      * @return descriptor or nullptr.
151      * @since 6
152      *
153      */
154     GattDescriptor *GetDescriptor(const UUID &uuid);
155 
156     /**
157      * @brief The function to get descriptors.
158      *
159      * @return list of descriptors.
160      * @since 6
161      *
162      */
163     std::vector<GattDescriptor> &GetDescriptors();
164 
165     /**
166      * @brief The function to get handle.
167      *
168      * @return uint16_t  handle.
169      * @since 6
170      *
171      */
172     uint16_t GetHandle() const;
173 
174     /**
175      * @brief The function to get permissions.
176      *
177      * @return permissions.
178      * @since 6
179      *
180      */
181     int GetPermissions() const;
182 
183     /**
184      * @brief The function to get properties.
185      *
186      * @return properties.
187      * @since 6
188      *
189      */
190     int GetProperties() const;
191 
192     /**
193      * @brief The function to get service.
194      *
195      * @return service which characteristic belong to.
196      * @since 6
197      *
198      */
199     GattService *GetService() const;
200 
201     /**
202      * @brief The function to get uuid.
203      *
204      * @return UUID.
205      * @since 6
206      *
207      */
208     const UUID &GetUuid() const;
209 
210     /**
211      * @brief The function to get value.
212      *
213      * @param size size of get value.
214      * @return value pointer.
215      * @since 6
216      *
217      */
218     const std::unique_ptr<uint8_t[]> &GetValue(size_t *size) const;
219 
220     /**
221      * @brief The function to get write type.
222      *
223      * @return write type.
224      * @since 6
225      *
226      */
227     int GetWriteType() const;
228 
229     /**
230      * @brief The function to set write type.
231      *
232      * @param type type of set write type.
233      * @return result of #GATT_STATUS.
234      * @since 6
235      *
236      */
237     int SetWriteType(int type);
238 
239     /**
240      * @brief The function to set value.
241      *
242      * @param values values of set value.
243      * @param length length of set value.
244      * @since 6
245      *
246      */
247     void SetValue(const uint8_t *values, const size_t length);
248 
249 private:
250     /**
251      * @brief The writeType of characteristic.
252      *
253      * @since 6
254      *
255      */
256     uint8_t writeType_;
257 
258     /**
259      * @brief The handle of characteristic.
260      *
261      * @since 6
262      *
263      */
264     uint16_t handle_;
265 
266     /**
267      * @brief The permissions of characteristic.
268      *
269      * @since 6
270      *
271      */
272     int permissions_;
273 
274     /**
275      * @brief The properties of characteristic.
276      *
277      * @since 6
278      *
279      */
280     int properties_;
281 
282     /**
283      * @brief The service of characteristic.
284      *
285      * @since 6
286      *
287      */
288     GattService *service_;
289 
290     /**
291      * @brief The value of characteristic.
292      *
293      * @since 6
294      *
295      */
296     std::unique_ptr<uint8_t[]> value_;
297 
298     /**
299      * @brief The length of characteristic.
300      *
301      * @since 6
302      *
303      */
304     size_t length_;
305 
306     /**
307      * @brief The descriptors of characteristic.
308      *
309      * @since 6
310      *
311      */
312     std::vector<GattDescriptor> descriptors_;
313 
314     /**
315      * @brief The uuid of characteristic.
316      *
317      * @since 6
318      *
319      */
320     UUID uuid_;
321 
322     friend class GattService;
323 };
324 
325 } // namespace Bluetooth
326 } // namespace OHOS
327 
328 #endif  // BLUETOOTH_GATT_CHARACTERISTIC_H
329