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