• 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_client.h
29  *
30  * @brief Bluetooth gatt client interface.
31  *
32  * @since 6
33  *
34  */
35 
36 #ifndef BLUETOOTH_GATT_CLIENT_H
37 #define BLUETOOTH_GATT_CLIENT_H
38 
39 #include "bluetooth_def.h"
40 #include "bluetooth_gatt_service.h"
41 #include "bluetooth_remote_device.h"
42 #include <memory>
43 
44 namespace OHOS {
45 namespace Bluetooth {
46 
47 /**
48  * @brief Class for GattClientCallback functions.
49  *
50  * @since 6
51  *
52  */
53 class GattClientCallback {
54 public:
55     /**
56      * @brief The function to OnConnectionStateChanged.
57      *
58      * @param connectionState callback of gattClientCallback.
59      * @param ret ret of GattClientCallback.
60      * @since 6
61      *
62      */
OnConnectionStateChanged(int connectionState,int ret)63     virtual void OnConnectionStateChanged(int connectionState, int ret)
64     {}
65 
66     /**
67      * @brief The function to OnConnectionStateChanged.
68      *
69      * @param connectionState callback of gattClientCallback.
70      * @param ret ret of GattClientCallback.
71      * @since 20
72      *
73      */
OnConnectionStateChangedWithReason(int connectionState,int ret,int disconnectReason)74     virtual void OnConnectionStateChangedWithReason(int connectionState, int ret, int disconnectReason)
75     {}
76 
77     /**
78      * @brief The function to OnCharacteristicChanged.
79      *
80      * @param characteristic Characteristic object to changed.
81      * @since 6
82      *
83      */
OnCharacteristicChanged(const GattCharacteristic & characteristic)84     virtual void OnCharacteristicChanged(const GattCharacteristic &characteristic)
85     {}
86 
87     /**
88      * @brief The function to OnCharacteristicReadResult.
89      *
90      * @param characteristic Characteristic object.
91      * @param ret ret of GattClientCallback.
92      * @since 6
93      *
94      */
OnCharacteristicReadResult(const GattCharacteristic & characteristic,int ret)95     virtual void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret)
96     {}
97 
OnReadRemoteRssiValueResult(int rssi,int status)98     virtual void OnReadRemoteRssiValueResult(int rssi, int status)
99     {}
100     /**
101      * @brief The function to OnCharacteristicWriteResult.
102      *
103      * @param characteristic Characteristic object. The value attribute is the initial value 0.
104      * @param ret ret of GattClientCallback.
105      * @since 6
106      *
107      */
OnCharacteristicWriteResult(const GattCharacteristic & characteristic,int ret)108     virtual void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret)
109     {}
110 
111     /**
112      * @brief The function to OnDescriptorReadResult.
113      *
114      * @param descriptor descriptor object.
115      * @param ret ret of GattClientCallback.
116      * @since 6
117      *
118      */
OnDescriptorReadResult(const GattDescriptor & descriptor,int ret)119     virtual void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret)
120     {}
121 
122     /**
123      * @brief The function to OnDescriptorWriteResult.
124      *
125      * @param descriptor descriptor object.
126      * @param ret ret of GattClientCallback.
127      * @since 6
128      *
129      */
OnDescriptorWriteResult(const GattDescriptor & descriptor,int ret)130     virtual void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret)
131     {}
132 
133     /**
134      * @brief The function to OnMtuUpdate.
135      *
136      * @param mtu mtu to update.
137      * @param ret ret of GattClientCallback.
138      * @since 6
139      *
140      */
OnMtuUpdate(int mtu,int ret)141     virtual void OnMtuUpdate(int mtu, int ret)
142     {}
143 
144     /**
145      * @brief The function to OnServicesDiscovered.
146      *
147      * @param status Status object.
148      * @since 6
149      *
150      */
OnServicesDiscovered(int status)151     virtual void OnServicesDiscovered(int status)
152     {}
153 
154     /**
155      * @brief The function to OnConnectionParameterChanged.
156      *
157      * @param interval interval object.
158      * @param latency latency object.
159      * @param timeout timeout object.
160      * @param status status object.
161      * @since 6
162      *
163      */
OnConnectionParameterChanged(int interval,int latency,int timeout,int status)164     virtual void OnConnectionParameterChanged(int interval, int latency, int timeout, int status)
165     {}
166 
167     /**
168      * @brief The function to OnSetNotifyCharacteristic.
169      *
170      * @param status status object.
171      * @since 6
172      *
173      */
OnSetNotifyCharacteristic(const GattCharacteristic & characteristic,int status)174     virtual void OnSetNotifyCharacteristic(const GattCharacteristic &characteristic, int status)
175     {}
176 
177     /**
178      * @brief A destructor of GattClientCallback.
179      *
180      * @since 6
181      *
182      */
~GattClientCallback()183     virtual ~GattClientCallback()
184     {}
185 };
186 
187 /**
188  * @brief Class for GattClient functions.
189  *
190  * @since 6
191  *
192  */
193 class BLUETOOTH_API GattClient : public std::enable_shared_from_this<GattClient> {
194 public:
195 
196     /**
197      * @brief init gattClient.
198      *
199      * @return init   api init result.
200      * @since 6
201      *
202      */
203     bool Init();
204 
205     /**
206      * @brief The function to Connect.
207      *
208      * @param callback callback of gattClientCallback.
209      * @param isAutoConnect isAutoConnect of GattClient.
210      * @param transport transport of GattClient.
211      * @return int   api accept status.
212      * @since 6
213      *
214      */
215     int Connect(std::weak_ptr<GattClientCallback> callback, bool isAutoConnect, int transport);
216 
217     /**
218      * @brief The function to request connection priority.
219      *
220      * @param connPriority connPriority of GattClient.
221      * @return int   api accept status.
222      * @since 6
223      *
224      */
225     int RequestConnectionPriority(int connPriority);
226 
227     /**
228      * @brief The function to request fastest connection.
229      *
230      * @return int   api accept status.
231      */
232     int RequestFastestConn();
233 
234     /**
235      * @brief The function to disconnect.
236      *
237      * @return int   api accept status.
238      * @since 6
239      *
240      */
241     int Disconnect();
242 
243     /**
244      * @brief The function to close.
245      *
246      * @return int.
247      * @since 6
248      *
249      */
250     int Close();
251 
252     /**
253      * @brief The function to discover services.
254      *
255      * @return int    api accept status.
256      * @since 6
257      *
258      */
259     int DiscoverServices();
260 
261     /**
262      * @brief The function to get service.
263      *
264      * @param uuid uuid of GattClient.
265      * @return service.
266      * @since 6
267      *
268      */
269     std::optional<std::reference_wrapper<GattService>> GetService(const UUID &uuid);
270 
271     /**
272      * @brief The function to get service.
273      *
274      * @return list of services.
275      * @since 6
276      *
277      */
278     std::vector<GattService> &GetService();
279 
280     /**
281      * @brief The function to read characteristic.
282      *
283      * @param characteristic Characteristic object.
284      * @return int read characteristic.
285      * @since 6
286      *
287      */
288     int ReadCharacteristic(GattCharacteristic &characteristic);
289 
290     /**
291      * @brief The function to read descriptor.
292      *
293      * @param descriptor descriptor object.
294      * @return int read descriptor.
295      * @since 6
296      *
297      */
298     int ReadDescriptor(GattDescriptor &descriptor);
299 
300     /**
301      * @brief The function to RequestBleMtuSize.
302      *
303      * @param mtu mtu of GattClient.
304      * @return int request ble mtu size.
305      * @since 6
306      *
307      */
308     int RequestBleMtuSize(int mtu);
309 
310     /**
311      * @brief The function to SetNotifyCharacteristic.
312      *
313      * @param characteristic characteristic object.
314      * @param enable enable of GattClient.
315      * @return result of #GATT_STATUS.
316      * @since 6
317      *
318      */
319     int SetNotifyCharacteristic(GattCharacteristic &characteristic, bool enable);
320 
321     /**
322      * @brief The function to SetNotifyCharacteristic version 2, this funtion will not trigger onDespritor callback.
323      *
324      * @param characteristic characteristic object.
325      * @param enable enable of GattClient.
326      * @return result of #GATT_STATUS.
327      * @since 13
328      *
329      */
330     int SetNotifyCharacteristicV2(GattCharacteristic &characteristic, bool enable);
331 
332     /**
333      * @brief The function to SetIndicateCharacteristic.
334      *
335      * @param characteristic characteristic object.
336      * @param enable enable of GattClient.
337      * @return result of #GATT_STATUS.
338      * @since 6
339      *
340      */
341     int SetIndicateCharacteristic(GattCharacteristic &characteristic, bool enable);
342 
343     /**
344      * @brief The function to write characteristic.
345      *
346      * @param characteristic characteristic object.
347      * @return int write characteristic.
348      * @since 6
349      *
350      */
351     int WriteCharacteristic(GattCharacteristic &characteristic);
352 
353     /**
354      * @brief The function to write characteristic.
355      *
356      * @param characteristic characteristic object.
357      * @param value characteristic value.
358      * @return int write characteristic.
359      * @since 6
360      *
361      */
362     int WriteCharacteristic(GattCharacteristic &characteristic, std::vector<uint8_t> value);
363 
364     /**
365      * @brief The function to write characteristic.
366      *
367      * @param descriptor descriptor object.
368      * @return int write descriptor.
369      * @since 6
370      *
371      */
372     int WriteDescriptor(GattDescriptor &descriptor);
373 
374     int ReadRemoteRssiValue();
375     /**
376      * @brief A constructor of GattClient.
377      *
378      * @param device Remote device object.
379      * @since 6
380      *
381      */
382     explicit GattClient(const BluetoothRemoteDevice &device);
383 
384     /**
385      * @brief A destructor of GattClient.
386      *
387      * @since 6
388      *
389      */
390     ~GattClient();
391 
392     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(GattClient);
393 
394 private:
395     BLUETOOTH_DECLARE_IMPL();
396 
397     int SetNotifyCharacteristicInner(GattCharacteristic &characteristic, bool enable,
398         const std::vector<uint8_t> &descriptorValue);
399 };
400 } // namespace Bluetooth
401 } // namespace OHOS
402 #endif  // BLUETOOTH_GATT_CLIENT_H
403