1 /******************************************************************************
2  *
3  *  Copyright 2003-2013 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This is the public interface file for BTA GATT.
22  *
23  ******************************************************************************/
24 
25 #ifndef BTA_GATT_API_H
26 #define BTA_GATT_API_H
27 
28 #include <base/functional/callback_forward.h>
29 #include <base/strings/stringprintf.h>
30 #include <bluetooth/log.h>
31 
32 #include <list>
33 #include <string>
34 #include <vector>
35 
36 #include "bta/gatt/database.h"
37 #include "hardware/bt_gatt_types.h"
38 #include "macros.h"
39 #include "stack/include/gatt_api.h"
40 #include "types/bluetooth/uuid.h"
41 #include "types/raw_address.h"
42 
43 #ifndef BTA_GATT_DEBUG
44 #define BTA_GATT_DEBUG false
45 #endif
46 
47 /*****************************************************************************
48  *  Constants and data types
49  ****************************************************************************/
50 /**************************
51  *  Common Definitions
52  **************************/
53 /* GATT ID */
54 typedef struct {
55   bluetooth::Uuid uuid; /* uuid of the attribute */
56   uint8_t inst_id;      /* instance ID */
57 } __attribute__((packed)) tBTA_GATT_ID;
58 
59 /* Client callback function events */
60 typedef enum : uint8_t {
61   BTA_GATTC_DEREG_EVT = 1,          /* GATT client deregistered event */
62   BTA_GATTC_OPEN_EVT = 2,           /* GATTC open request status  event */
63   BTA_GATTC_CLOSE_EVT = 5,          /* GATTC  close request status event */
64   BTA_GATTC_SEARCH_CMPL_EVT = 6,    /* GATT discovery complete event */
65   BTA_GATTC_SEARCH_RES_EVT = 7,     /* GATT discovery result event */
66   BTA_GATTC_SRVC_DISC_DONE_EVT = 8, /* GATT service discovery done event */
67   BTA_GATTC_NOTIF_EVT = 10,         /* GATT attribute notification event */
68   BTA_GATTC_EXEC_EVT = 12,          /* execute write complete event */
69   BTA_GATTC_CANCEL_OPEN_EVT = 14,   /* cancel open event */
70   BTA_GATTC_SRVC_CHG_EVT = 15,      /* service change event */
71   BTA_GATTC_ENC_CMPL_CB_EVT = 17,   /* encryption complete callback event */
72   BTA_GATTC_CFG_MTU_EVT = 18,       /* configure MTU complete event */
73   BTA_GATTC_CONGEST_EVT = 24,       /* Congestion event */
74   BTA_GATTC_PHY_UPDATE_EVT = 25,    /* PHY change event */
75   BTA_GATTC_CONN_UPDATE_EVT = 26,   /* Connection parameters update event */
76   BTA_GATTC_SUBRATE_CHG_EVT = 27,   /* Subrate Change event */
77 } tBTA_GATTC_EVT;
78 
gatt_client_event_text(const tBTA_GATTC_EVT & event)79 inline std::string gatt_client_event_text(const tBTA_GATTC_EVT& event) {
80   switch (event) {
81     CASE_RETURN_TEXT(BTA_GATTC_DEREG_EVT);
82     CASE_RETURN_TEXT(BTA_GATTC_OPEN_EVT);
83     CASE_RETURN_TEXT(BTA_GATTC_CLOSE_EVT);
84     CASE_RETURN_TEXT(BTA_GATTC_SEARCH_CMPL_EVT);
85     CASE_RETURN_TEXT(BTA_GATTC_SEARCH_RES_EVT);
86     CASE_RETURN_TEXT(BTA_GATTC_SRVC_DISC_DONE_EVT);
87     CASE_RETURN_TEXT(BTA_GATTC_NOTIF_EVT);
88     CASE_RETURN_TEXT(BTA_GATTC_EXEC_EVT);
89     CASE_RETURN_TEXT(BTA_GATTC_CANCEL_OPEN_EVT);
90     CASE_RETURN_TEXT(BTA_GATTC_SRVC_CHG_EVT);
91     CASE_RETURN_TEXT(BTA_GATTC_ENC_CMPL_CB_EVT);
92     CASE_RETURN_TEXT(BTA_GATTC_CFG_MTU_EVT);
93     CASE_RETURN_TEXT(BTA_GATTC_CONGEST_EVT);
94     CASE_RETURN_TEXT(BTA_GATTC_PHY_UPDATE_EVT);
95     CASE_RETURN_TEXT(BTA_GATTC_CONN_UPDATE_EVT);
96     CASE_RETURN_TEXT(BTA_GATTC_SUBRATE_CHG_EVT);
97     default:
98       return base::StringPrintf("UNKNOWN[%hhu]", event);
99   }
100 }
101 
102 typedef struct {
103   uint16_t unit;  /* as UUIUD defined by SIG */
104   uint16_t descr; /* as UUID as defined by SIG */
105   tGATT_FORMAT format;
106   int8_t exp;
107   uint8_t name_spc; /* The name space of the description */
108 } tBTA_GATT_CHAR_PRES;
109 
110 /* Characteristic Aggregate Format attribute value
111  */
112 #define BTA_GATT_AGGR_HANDLE_NUM_MAX 10
113 typedef struct {
114   uint8_t num_handle;
115   uint16_t handle_list[BTA_GATT_AGGR_HANDLE_NUM_MAX];
116 } tBTA_GATT_CHAR_AGGRE;
117 
118 typedef struct {
119   uint16_t len;
120   uint8_t* p_value;
121 } tBTA_GATT_UNFMT;
122 
123 typedef struct {
124   uint8_t num_attr;
125   uint16_t handles[GATT_MAX_READ_MULTI_HANDLES];
126 } tBTA_GATTC_MULTI;
127 
128 /* callback data structure */
129 typedef struct {
130   tGATT_STATUS status;
131   tGATT_IF client_if;
132 } tBTA_GATTC_REG;
133 
134 typedef struct {
135   uint16_t conn_id;
136   tGATT_STATUS status;
137   uint16_t handle;
138   uint16_t len;
139   uint8_t value[GATT_MAX_ATTR_LEN];
140 } tBTA_GATTC_READ;
141 
142 typedef struct {
143   uint16_t conn_id;
144   tGATT_STATUS status;
145   uint16_t handle;
146 } tBTA_GATTC_WRITE;
147 
148 typedef struct {
149   uint16_t conn_id;
150   tGATT_STATUS status;
151 } tBTA_GATTC_EXEC_CMPL;
152 
153 typedef struct {
154   uint16_t conn_id;
155   tGATT_STATUS status;
156 } tBTA_GATTC_SEARCH_CMPL;
157 
158 typedef struct {
159   uint16_t conn_id;
160   tBTA_GATT_ID service_uuid;
161 } tBTA_GATTC_SRVC_RES;
162 
163 typedef struct {
164   uint16_t conn_id;
165   tGATT_STATUS status;
166   uint16_t mtu;
167 } tBTA_GATTC_CFG_MTU;
168 
169 typedef struct {
170   tGATT_STATUS status;
171   uint16_t conn_id;
172   tGATT_IF client_if;
173   RawAddress remote_bda;
174   tBT_TRANSPORT transport;
175   uint16_t mtu;
176 } tBTA_GATTC_OPEN;
177 
178 typedef struct {
179   uint16_t conn_id;
180   tGATT_STATUS status;
181   tGATT_IF client_if;
182   RawAddress remote_bda;
183   tGATT_DISCONN_REASON reason;
184 } tBTA_GATTC_CLOSE;
185 
186 typedef struct {
187   uint16_t conn_id;
188   RawAddress bda;
189   uint16_t handle;
190   uint16_t len;
191   uint8_t value[GATT_MAX_ATTR_LEN];
192   bool is_notify;
193   uint16_t cid;
194 } tBTA_GATTC_NOTIFY;
195 
196 typedef struct {
197   uint16_t conn_id;
198   bool congested; /* congestion indicator */
199 } tBTA_GATTC_CONGEST;
200 
201 typedef struct {
202   tGATT_STATUS status;
203   tGATT_IF client_if;
204   uint16_t conn_id;
205   RawAddress remote_bda;
206 } tBTA_GATTC_OPEN_CLOSE;
207 
208 typedef struct {
209   tGATT_IF client_if;
210   RawAddress remote_bda;
211 } tBTA_GATTC_ENC_CMPL_CB;
212 
213 typedef struct {
214   tGATT_IF server_if;
215   uint16_t conn_id;
216   uint8_t tx_phy;
217   uint8_t rx_phy;
218   tGATT_STATUS status;
219 } tBTA_GATTC_PHY_UPDATE;
220 
221 typedef struct {
222   tGATT_IF server_if;
223   uint16_t conn_id;
224   uint16_t interval;
225   uint16_t latency;
226   uint16_t timeout;
227   tGATT_STATUS status;
228 } tBTA_GATTC_CONN_UPDATE;
229 
230 typedef struct {
231   RawAddress remote_bda;
232   uint16_t conn_id;
233 } tBTA_GATTC_SERVICE_CHANGED;
234 
235 typedef struct {
236   tGATT_IF server_if;
237   uint16_t conn_id;
238   uint16_t subrate_factor;
239   uint16_t latency;
240   uint16_t cont_num;
241   uint16_t timeout;
242   tGATT_STATUS status;
243 } tBTA_GATTC_SUBRATE_CHG;
244 
245 typedef union {
246   tGATT_STATUS status;
247 
248   tBTA_GATTC_SEARCH_CMPL search_cmpl; /* discovery complete */
249   tBTA_GATTC_SRVC_RES srvc_res;       /* discovery result */
250   tBTA_GATTC_REG reg_oper;            /* registration data */
251   tBTA_GATTC_OPEN open;
252   tBTA_GATTC_CLOSE close;
253   tBTA_GATTC_READ read;           /* read attribute/descriptor data */
254   tBTA_GATTC_WRITE write;         /* write complete data */
255   tBTA_GATTC_EXEC_CMPL exec_cmpl; /*  execute complete */
256   tBTA_GATTC_NOTIFY notify;       /* notification/indication event data */
257   tBTA_GATTC_ENC_CMPL_CB enc_cmpl;
258   RawAddress remote_bda;      /* service change event */
259   tBTA_GATTC_CFG_MTU cfg_mtu; /* configure MTU operation */
260   tBTA_GATTC_CONGEST congest;
261   tBTA_GATTC_PHY_UPDATE phy_update;
262   tBTA_GATTC_CONN_UPDATE conn_update;
263   tBTA_GATTC_SERVICE_CHANGED service_changed;
264   tBTA_GATTC_SUBRATE_CHG subrate_chg;
265 } tBTA_GATTC;
266 
267 /* GATTC enable callback function */
268 typedef void(tBTA_GATTC_ENB_CBACK)(tGATT_STATUS status);
269 
270 /* Client callback function */
271 typedef void(tBTA_GATTC_CBACK)(tBTA_GATTC_EVT event, tBTA_GATTC* p_data);
272 
273 /* GATT Server Data Structure */
274 /* Server callback function events */
275 #define BTA_GATTS_REG_EVT 0
276 #define BTA_GATTS_READ_CHARACTERISTIC_EVT \
277   GATTS_REQ_TYPE_READ_CHARACTERISTIC                                 /* 1 */
278 #define BTA_GATTS_READ_DESCRIPTOR_EVT GATTS_REQ_TYPE_READ_DESCRIPTOR /* 2 */
279 #define BTA_GATTS_WRITE_CHARACTERISTIC_EVT \
280   GATTS_REQ_TYPE_WRITE_CHARACTERISTIC                                  /* 3 */
281 #define BTA_GATTS_WRITE_DESCRIPTOR_EVT GATTS_REQ_TYPE_WRITE_DESCRIPTOR /* 4 */
282 #define BTA_GATTS_EXEC_WRITE_EVT GATTS_REQ_TYPE_WRITE_EXEC             /* 5 */
283 #define BTA_GATTS_MTU_EVT GATTS_REQ_TYPE_MTU                           /* 6 */
284 #define BTA_GATTS_CONF_EVT GATTS_REQ_TYPE_CONF                         /* 7 */
285 #define BTA_GATTS_DEREG_EVT 8
286 #define BTA_GATTS_DELELTE_EVT 11
287 #define BTA_GATTS_STOP_EVT 13
288 #define BTA_GATTS_CONNECT_EVT 14
289 #define BTA_GATTS_DISCONNECT_EVT 15
290 #define BTA_GATTS_OPEN_EVT 16
291 #define BTA_GATTS_CANCEL_OPEN_EVT 17
292 #define BTA_GATTS_CLOSE_EVT 18
293 #define BTA_GATTS_CONGEST_EVT 20
294 #define BTA_GATTS_PHY_UPDATE_EVT 21
295 #define BTA_GATTS_CONN_UPDATE_EVT 22
296 #define BTA_GATTS_SUBRATE_CHG_EVT 23
297 
298 typedef uint8_t tBTA_GATTS_EVT;
299 
gatt_server_event_text(const tBTA_GATTS_EVT & event)300 inline std::string gatt_server_event_text(const tBTA_GATTS_EVT& event) {
301   switch (event) {
302     CASE_RETURN_TEXT(BTA_GATTS_REG_EVT);
303     CASE_RETURN_TEXT(BTA_GATTS_READ_CHARACTERISTIC_EVT);
304     CASE_RETURN_TEXT(BTA_GATTS_READ_DESCRIPTOR_EVT);
305     CASE_RETURN_TEXT(BTA_GATTS_WRITE_CHARACTERISTIC_EVT);
306     CASE_RETURN_TEXT(BTA_GATTS_WRITE_DESCRIPTOR_EVT);
307     CASE_RETURN_TEXT(BTA_GATTS_EXEC_WRITE_EVT);
308     CASE_RETURN_TEXT(BTA_GATTS_MTU_EVT);
309     CASE_RETURN_TEXT(BTA_GATTS_CONF_EVT);
310     CASE_RETURN_TEXT(BTA_GATTS_DEREG_EVT);
311     CASE_RETURN_TEXT(BTA_GATTS_DELELTE_EVT);
312     CASE_RETURN_TEXT(BTA_GATTS_STOP_EVT);
313     CASE_RETURN_TEXT(BTA_GATTS_CONNECT_EVT);
314     CASE_RETURN_TEXT(BTA_GATTS_DISCONNECT_EVT);
315     CASE_RETURN_TEXT(BTA_GATTS_OPEN_EVT);
316     CASE_RETURN_TEXT(BTA_GATTS_CANCEL_OPEN_EVT);
317     CASE_RETURN_TEXT(BTA_GATTS_CLOSE_EVT);
318     CASE_RETURN_TEXT(BTA_GATTS_CONGEST_EVT);
319     CASE_RETURN_TEXT(BTA_GATTS_PHY_UPDATE_EVT);
320     CASE_RETURN_TEXT(BTA_GATTS_CONN_UPDATE_EVT);
321     CASE_RETURN_TEXT(BTA_GATTS_SUBRATE_CHG_EVT);
322     default:
323       return base::StringPrintf("UNKNOWN[%hhu]", event);
324   }
325 }
326 
327 #define BTA_GATTS_INVALID_APP 0xff
328 
329 #define BTA_GATTS_INVALID_IF 0
330 
331 #ifndef BTA_GATTC_CHAR_DESCR_MAX
332 #define BTA_GATTC_CHAR_DESCR_MAX 7
333 #endif
334 
335 /***********************  NV callback Data Definitions   **********************
336  */
337 typedef struct {
338   bluetooth::Uuid app_uuid128;
339   bluetooth::Uuid svc_uuid;
340   uint16_t svc_inst;
341   uint16_t s_handle;
342   uint16_t e_handle;
343   bool is_primary; /* primary service or secondary */
344 } tBTA_GATTS_HNDL_RANGE;
345 
346 typedef struct {
347   tGATT_STATUS status;
348   RawAddress remote_bda;
349   uint32_t trans_id;
350   uint16_t conn_id;
351   tGATTS_DATA* p_data;
352 } tBTA_GATTS_REQ;
353 
354 typedef struct {
355   tGATT_IF server_if;
356   tGATT_STATUS status;
357   bluetooth::Uuid uuid;
358 } tBTA_GATTS_REG_OPER;
359 
360 typedef struct {
361   tGATT_IF server_if;
362   uint16_t service_id;
363   uint16_t svc_instance;
364   bool is_primary;
365   tGATT_STATUS status;
366   bluetooth::Uuid uuid;
367 } tBTA_GATTS_CREATE;
368 
369 typedef struct {
370   tGATT_IF server_if;
371   uint16_t service_id;
372   tGATT_STATUS status;
373 } tBTA_GATTS_SRVC_OPER;
374 
375 typedef struct {
376   tGATT_IF server_if;
377   RawAddress remote_bda;
378   uint16_t conn_id;
379   tBT_TRANSPORT transport;
380 } tBTA_GATTS_CONN;
381 
382 typedef struct {
383   uint16_t conn_id;
384   bool congested; /* report channel congestion indicator */
385 } tBTA_GATTS_CONGEST;
386 
387 typedef struct {
388   uint16_t conn_id;        /* connection ID */
389   tGATT_STATUS status;     /* notification/indication status */
390 } tBTA_GATTS_CONF;
391 
392 typedef struct {
393   tGATT_IF server_if;
394   uint16_t conn_id;
395   uint8_t tx_phy;
396   uint8_t rx_phy;
397   tGATT_STATUS status;
398 } tBTA_GATTS_PHY_UPDATE;
399 
400 typedef struct {
401   tGATT_IF server_if;
402   uint16_t conn_id;
403   uint16_t interval;
404   uint16_t latency;
405   uint16_t timeout;
406   tGATT_STATUS status;
407 } tBTA_GATTS_CONN_UPDATE;
408 
409 typedef struct {
410   tGATT_IF server_if;
411   uint16_t conn_id;
412   uint16_t subrate_factor;
413   uint16_t latency;
414   uint16_t cont_num;
415   uint16_t timeout;
416   tGATT_STATUS status;
417 } tBTA_GATTS_SUBRATE_CHG;
418 
419 /* GATTS callback data */
420 typedef union {
421   tBTA_GATTS_REG_OPER reg_oper;
422   tBTA_GATTS_CREATE create;
423   tBTA_GATTS_SRVC_OPER srvc_oper;
424   tGATT_STATUS status; /* BTA_GATTS_LISTEN_EVT */
425   tBTA_GATTS_REQ req_data;
426   tBTA_GATTS_CONN conn;             /* BTA_GATTS_CONN_EVT */
427   tBTA_GATTS_CONGEST congest;       /* BTA_GATTS_CONGEST_EVT callback data */
428   tBTA_GATTS_CONF confirm;          /* BTA_GATTS_CONF_EVT callback data */
429   tBTA_GATTS_PHY_UPDATE phy_update; /* BTA_GATTS_PHY_UPDATE_EVT callback data */
430   tBTA_GATTS_CONN_UPDATE
431       conn_update; /* BTA_GATTS_CONN_UPDATE_EVT callback data */
432   tBTA_GATTS_SUBRATE_CHG subrate_chg; /* BTA_GATTS_SUBRATE_CHG_EVT */
433 } tBTA_GATTS;
434 
435 /* GATTS enable callback function */
436 typedef void(tBTA_GATTS_ENB_CBACK)(tGATT_STATUS status);
437 
438 /* Server callback function */
439 typedef void(tBTA_GATTS_CBACK)(tBTA_GATTS_EVT event, tBTA_GATTS* p_data);
440 
441 /*****************************************************************************
442  *  External Function Declarations
443  ****************************************************************************/
444 
445 /**************************
446  *  Client Functions
447  **************************/
448 
449 /*******************************************************************************
450  *
451  * Function         BTA_GATTC_Disable
452  *
453  * Description      This function is called to disable the GATTC module
454  *
455  * Parameters       None.
456  *
457  * Returns          None
458  *
459  ******************************************************************************/
460 void BTA_GATTC_Disable(void);
461 
462 using BtaAppRegisterCallback =
463     base::Callback<void(uint8_t /* app_id */, uint8_t /* status */)>;
464 
465 /**
466  * This function is called to register application callbacks with BTA GATTC
467  *module.
468  * p_client_cb - pointer to the application callback function.
469  **/
470 void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb,
471                            BtaAppRegisterCallback cb, bool eatt_support);
472 
473 /*******************************************************************************
474  *
475  * Function         BTA_GATTC_AppDeregister
476  *
477  * Description      This function is called to deregister an application
478  *                  from BTA GATTC module.
479  *
480  * Parameters       client_if - client interface identifier.
481  *
482  * Returns          None
483  *
484  ******************************************************************************/
485 void BTA_GATTC_AppDeregister(tGATT_IF client_if);
486 
487 /*******************************************************************************
488  *
489  * Function         BTA_GATTC_Open
490  *
491  * Description      Open a direct connection or add a background auto connection
492  *                  bd address
493  *
494  * Parameters       client_if: server interface.
495  *                  remote_bda: remote device BD address.
496  *                  connection_type: connection type used for the peer device
497  *                  initiating_phys: LE PHY to use, optional
498  *
499  ******************************************************************************/
500 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
501                     tBTM_BLE_CONN_TYPE connection_type, bool opportunistic);
502 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
503                     tBTM_BLE_CONN_TYPE connection_type, tBT_TRANSPORT transport,
504                     bool opportunistic, uint8_t initiating_phys);
505 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
506                     tBLE_ADDR_TYPE addr_type,
507                     tBTM_BLE_CONN_TYPE connection_type, tBT_TRANSPORT transport,
508                     bool opportunistic, uint8_t initiating_phys);
509 
510 /*******************************************************************************
511  *
512  * Function         BTA_GATTC_CancelOpen
513  *
514  * Description      Open a direct connection or add a background auto connection
515  *                  bd address
516  *
517  * Parameters       client_if: server interface.
518  *                  remote_bda: remote device BD address.
519  *                  is_direct: direct connection or background auto connection
520  *
521  * Returns          void
522  *
523  ******************************************************************************/
524 void BTA_GATTC_CancelOpen(tGATT_IF client_if, const RawAddress& remote_bda,
525                           bool is_direct);
526 
527 /*******************************************************************************
528  *
529  * Function         BTA_GATTC_Close
530  *
531  * Description      Close a connection to a GATT server.
532  *
533  * Parameters       conn_id: connectino ID to be closed.
534  *
535  * Returns          void
536  *
537  ******************************************************************************/
538 void BTA_GATTC_Close(uint16_t conn_id);
539 
540 /*******************************************************************************
541  *
542  * Function         BTA_GATTC_ServiceSearchAllRequest
543  *
544  * Description      This function is called to request a GATT service discovery
545  *                  of all services on a GATT server. This function report
546  *                  service search result by a callback event, and followed by a
547  *                  service search complete event.
548  *
549  * Parameters       conn_id: connection ID.
550  *
551  * Returns          None
552  *
553  ******************************************************************************/
554 void BTA_GATTC_ServiceSearchAllRequest(uint16_t conn_id);
555 
556 /*******************************************************************************
557  *
558  * Function         BTA_GATTC_ServiceSearchRequest
559  *
560  * Description      This function is called to request a GATT service discovery
561  *                  on a GATT server. This function report service search result
562  *                  by a callback event, and followed by a service search
563  *                  complete event.
564  *
565  * Parameters       conn_id: connection ID.
566  *                  p_srvc_uuid: a UUID of the service application is interested
567  *                               in.
568  * Returns          None
569  *
570  ******************************************************************************/
571 void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id,
572                                     bluetooth::Uuid p_srvc_uuid);
573 
574 /**
575  * This function is called to send "Find service by UUID" request. Used only for
576  * PTS tests.
577  */
578 void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id,
579                                      const bluetooth::Uuid& srvc_uuid);
580 
581 /*******************************************************************************
582  *
583  * Function         BTA_GATTC_GetServices
584  *
585  * Description      This function is called to find the services on the given
586  *                  server.
587  *
588  * Parameters       conn_id: connection ID which identify the server.
589  *
590  * Returns          returns list of gatt::Service or NULL.
591  *
592  ******************************************************************************/
593 const std::list<gatt::Service>* BTA_GATTC_GetServices(uint16_t conn_id);
594 
595 /*******************************************************************************
596  *
597  * Function         BTA_GATTC_GetCharacteristic
598  *
599  * Description      This function is called to find the characteristic on the
600  *                  given server.
601  *
602  * Parameters       conn_id: connection ID which identify the server.
603  *                  handle: characteristic handle
604  *
605  * Returns          returns pointer to gatt::Characteristic or NULL.
606  *
607  ******************************************************************************/
608 const gatt::Characteristic* BTA_GATTC_GetCharacteristic(uint16_t conn_id,
609                                                         uint16_t handle);
610 
611 /*******************************************************************************
612  *
613  * Function         BTA_GATTC_GetDescriptor
614  *
615  * Description      This function is called to find the characteristic on the
616  *                  given server.
617  *
618  * Parameters       conn_id: connection ID which identify the server.
619  *                  handle: descriptor handle
620  *
621  * Returns          returns pointer to gatt::Descriptor or NULL.
622  *
623  ******************************************************************************/
624 const gatt::Descriptor* BTA_GATTC_GetDescriptor(uint16_t conn_id,
625                                                 uint16_t handle);
626 
627 /* Return characteristic that owns descriptor with handle equal to |handle|, or
628  * NULL */
629 const gatt::Characteristic* BTA_GATTC_GetOwningCharacteristic(uint16_t conn_id,
630                                                               uint16_t handle);
631 
632 /* Return service that owns descriptor or characteristic with handle equal to
633  * |handle|, or NULL */
634 const gatt::Service* BTA_GATTC_GetOwningService(uint16_t conn_id,
635                                                 uint16_t handle);
636 
637 /*******************************************************************************
638  *
639  * Function         BTA_GATTC_GetGattDb
640  *
641  * Description      This function is called to get gatt db.
642  *
643  * Parameters       conn_id: connection ID which identify the server.
644  *                  db: output parameter which will contain gatt db copy.
645  *                      Caller is responsible for freeing it.
646  *                  count: number of elements in db.
647  *
648  ******************************************************************************/
649 void BTA_GATTC_GetGattDb(uint16_t conn_id, uint16_t start_handle,
650                          uint16_t end_handle, btgatt_db_element_t** db,
651                          int* count);
652 
653 typedef void (*GATT_READ_OP_CB)(uint16_t conn_id, tGATT_STATUS status,
654                                 uint16_t handle, uint16_t len, uint8_t* value,
655                                 void* data);
656 typedef void (*GATT_WRITE_OP_CB)(uint16_t conn_id, tGATT_STATUS status,
657                                  uint16_t handle, uint16_t len,
658                                  const uint8_t* value, void* data);
659 typedef void (*GATT_CONFIGURE_MTU_OP_CB)(uint16_t conn_id, tGATT_STATUS status,
660                                          void* data);
661 typedef void (*GATT_READ_MULTI_OP_CB)(uint16_t conn_id, tGATT_STATUS status,
662                                       tBTA_GATTC_MULTI& handles, uint16_t len,
663                                       uint8_t* value, void* data);
664 /*******************************************************************************
665  *
666  * Function         BTA_GATTC_ReadCharacteristic
667  *
668  * Description      This function is called to read a characteristics value
669  *
670  * Parameters       conn_id - connectino ID.
671  *                  handle - characteritic handle to read.
672  *
673  * Returns          None
674  *
675  ******************************************************************************/
676 void BTA_GATTC_ReadCharacteristic(uint16_t conn_id, uint16_t handle,
677                                   tGATT_AUTH_REQ auth_req,
678                                   GATT_READ_OP_CB callback, void* cb_data);
679 
680 /**
681  * This function is called to read a value of characteristic with uuid equal to
682  * |uuid|
683  */
684 void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, const bluetooth::Uuid& uuid,
685                                  uint16_t s_handle, uint16_t e_handle,
686                                  tGATT_AUTH_REQ auth_req,
687                                  GATT_READ_OP_CB callback, void* cb_data);
688 
689 /*******************************************************************************
690  *
691  * Function         BTA_GATTC_ReadCharDescr
692  *
693  * Description      This function is called to read a descriptor value.
694  *
695  * Parameters       conn_id - connection ID.
696  *                  handle - descriptor handle to read.
697  *
698  * Returns          None
699  *
700  ******************************************************************************/
701 void BTA_GATTC_ReadCharDescr(uint16_t conn_id, uint16_t handle,
702                              tGATT_AUTH_REQ auth_req, GATT_READ_OP_CB callback,
703                              void* cb_data);
704 
705 /*******************************************************************************
706  *
707  * Function         BTA_GATTC_WriteCharValue
708  *
709  * Description      This function is called to write characteristic value.
710  *
711  * Parameters       conn_id - connection ID.
712  *                  handle - characteristic handle to write.
713  *                  write_type - type of write.
714  *                  value - the value to be written.
715  *
716  * Returns          None
717  *
718  ******************************************************************************/
719 void BTA_GATTC_WriteCharValue(uint16_t conn_id, uint16_t handle,
720                               tGATT_WRITE_TYPE write_type,
721                               std::vector<uint8_t> value,
722                               tGATT_AUTH_REQ auth_req,
723                               GATT_WRITE_OP_CB callback, void* cb_data);
724 
725 /*******************************************************************************
726  *
727  * Function         BTA_GATTC_WriteCharDescr
728  *
729  * Description      This function is called to write descriptor value.
730  *
731  * Parameters       conn_id - connection ID
732  *                  handle - descriptor handle to write.
733  *                  value - the value to be written.
734  *
735  * Returns          None
736  *
737  ******************************************************************************/
738 void BTA_GATTC_WriteCharDescr(uint16_t conn_id, uint16_t handle,
739                               std::vector<uint8_t> value,
740                               tGATT_AUTH_REQ auth_req,
741                               GATT_WRITE_OP_CB callback, void* cb_data);
742 
743 /*******************************************************************************
744  *
745  * Function         BTA_GATTC_SendIndConfirm
746  *
747  * Description      This function is called to send handle value confirmation.
748  *
749  * Parameters       conn_id - connection ID.
750  *                  cid - channel id
751  *
752  * Returns          None
753  *
754  ******************************************************************************/
755 void BTA_GATTC_SendIndConfirm(uint16_t conn_id, uint16_t cid);
756 
757 /*******************************************************************************
758  *
759  * Function         BTA_GATTC_RegisterForNotifications
760  *
761  * Description      This function is called to register for notification of a
762  *                  service.
763  *
764  * Parameters       client_if - client interface.
765  *                  remote_bda - target GATT server.
766  *                  handle - GATT characteristic handle.
767  *
768  * Returns          OK if registration succeed, otherwise failed.
769  *
770  ******************************************************************************/
771 tGATT_STATUS BTA_GATTC_RegisterForNotifications(tGATT_IF client_if,
772                                                 const RawAddress& remote_bda,
773                                                 uint16_t handle);
774 
775 /*******************************************************************************
776  *
777  * Function         BTA_GATTC_DeregisterForNotifications
778  *
779  * Description      This function is called to de-register for notification of a
780  *                  service.
781  *
782  * Parameters       client_if - client interface.
783  *                  remote_bda - target GATT server.
784  *                  handle - GATT characteristic handle.
785  *
786  * Returns          OK if deregistration succeed, otherwise failed.
787  *
788  ******************************************************************************/
789 tGATT_STATUS BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if,
790                                                   const RawAddress& remote_bda,
791                                                   uint16_t handle);
792 
793 /*******************************************************************************
794  *
795  * Function         BTA_GATTC_PrepareWrite
796  *
797  * Description      This function is called to prepare write a characteristic
798  *                  value.
799  *
800  * Parameters       conn_id - connection ID.
801  *                  handle - GATT characteritic handle.
802  *                  offset - offset of the write value.
803  *                  value - the value to be written.
804  *
805  * Returns          None
806  *
807  ******************************************************************************/
808 void BTA_GATTC_PrepareWrite(uint16_t conn_id, uint16_t handle, uint16_t offset,
809                             std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req,
810                             GATT_WRITE_OP_CB callback, void* cb_data);
811 
812 /*******************************************************************************
813  *
814  * Function         BTA_GATTC_ExecuteWrite
815  *
816  * Description      This function is called to execute write a prepare write
817  *                  sequence.
818  *
819  * Parameters       conn_id - connection ID.
820  *                    is_execute - execute or cancel.
821  *
822  * Returns          None
823  *
824  ******************************************************************************/
825 void BTA_GATTC_ExecuteWrite(uint16_t conn_id, bool is_execute);
826 
827 /*******************************************************************************
828  *
829  * Function         BTA_GATTC_ReadMultiple
830  *
831  * Description      This function is called to read multiple characteristic or
832  *                  characteristic descriptors.
833  *
834  * Parameters       conn_id - connectino ID.
835  *                  p_read_multi - read multiple parameters.
836  *                  variable_len - whether "read multi variable length" variant
837  *                                 shall be used.
838  *
839  * Returns          None
840  *
841  ******************************************************************************/
842 void BTA_GATTC_ReadMultiple(uint16_t conn_id, tBTA_GATTC_MULTI& p_read_multi,
843                             bool variable_len, tGATT_AUTH_REQ auth_req,
844                             GATT_READ_MULTI_OP_CB callback, void* cb_data);
845 
846 /*******************************************************************************
847  *
848  * Function         BTA_GATTC_Refresh
849  *
850  * Description      Refresh the server cache of the remote device
851  *
852  * Parameters       remote_bda: remote device BD address.
853  *
854  * Returns          void
855  *
856  ******************************************************************************/
857 void BTA_GATTC_Refresh(const RawAddress& remote_bda);
858 
859 /*******************************************************************************
860  *
861  * Function         BTA_GATTC_ConfigureMTU
862  *
863  * Description      Configure the MTU size in the GATT channel. This can be done
864  *                  only once per connection.
865  *
866  * Parameters       conn_id: connection ID.
867  *                  mtu: desired MTU size to use.
868  *
869  * Returns          void
870  *
871  ******************************************************************************/
872 void BTA_GATTC_ConfigureMTU(uint16_t conn_id, uint16_t mtu);
873 void BTA_GATTC_ConfigureMTU(uint16_t conn_id, uint16_t mtu,
874                             GATT_CONFIGURE_MTU_OP_CB callback, void* cb_data);
875 
876 /*******************************************************************************
877  *  BTA GATT Server API
878  ******************************************************************************/
879 
880 /*******************************************************************************
881  *
882  * Function         BTA_GATTS_Init
883  *
884  * Description      This function is called to initalize GATTS module
885  *
886  * Parameters       None
887  *
888  * Returns          None
889  *
890  ******************************************************************************/
891 void BTA_GATTS_Init();
892 
893 /*******************************************************************************
894  *
895  * Function         BTA_GATTS_Disable
896  *
897  * Description      This function is called to disable GATTS module
898  *
899  * Parameters       None.
900  *
901  * Returns          None
902  *
903  ******************************************************************************/
904 void BTA_GATTS_Disable(void);
905 
906 /*******************************************************************************
907  *
908  * Function         BTA_GATTS_AppRegister
909  *
910  * Description      This function is called to register application callbacks
911  *                    with BTA GATTS module.
912  *
913  * Parameters       p_app_uuid - applicaiton UUID
914  *                  p_cback - pointer to the application callback function.
915  *                  eatt_support: indicate eatt support.
916  *
917  * Returns          None
918  *
919  ******************************************************************************/
920 void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid,
921                            tBTA_GATTS_CBACK* p_cback, bool eatt_support);
922 
923 /*******************************************************************************
924  *
925  * Function         BTA_GATTS_AppDeregister
926  *
927  * Description      De-register with BTA GATT Server.
928  *
929  * Parameters       server_if: server interface
930  *
931  * Returns          void
932  *
933  ******************************************************************************/
934 void BTA_GATTS_AppDeregister(tGATT_IF server_if);
935 
936 /*******************************************************************************
937  *
938  * Function         BTA_GATTS_AddService
939  *
940  * Description      Add the given |service| and all included elements to the
941  *                  GATT database. a |BTA_GATTS_ADD_SRVC_EVT| is triggered to
942  *                  report the status and attribute handles.
943  *
944  * Parameters       server_if: server interface.
945  *                  service: pointer to vector describing service.
946  *
947  * Returns          Returns |GATT_SUCCESS| on success or |GATT_ERROR| if the
948  *                  service cannot be added.
949  *
950  ******************************************************************************/
951 typedef base::Callback<void(tGATT_STATUS status, int server_if,
952                             std::vector<btgatt_db_element_t> service)>
953     BTA_GATTS_AddServiceCb;
954 
955 void BTA_GATTS_AddService(tGATT_IF server_if,
956                           std::vector<btgatt_db_element_t> service,
957                           BTA_GATTS_AddServiceCb cb);
958 
959 /*******************************************************************************
960  *
961  * Function         BTA_GATTS_DeleteService
962  *
963  * Description      This function is called to delete a service. When this is
964  *                  done, a callback event BTA_GATTS_DELETE_EVT is report with
965  *                  the status.
966  *
967  * Parameters       service_id: service_id to be deleted.
968  *
969  * Returns          returns none.
970  *
971  ******************************************************************************/
972 void BTA_GATTS_DeleteService(uint16_t service_id);
973 
974 /*******************************************************************************
975  *
976  * Function         BTA_GATTS_StopService
977  *
978  * Description      This function is called to stop a service.
979  *
980  * Parameters       service_id - service to be topped.
981  *
982  * Returns          None
983  *
984  ******************************************************************************/
985 void BTA_GATTS_StopService(uint16_t service_id);
986 
987 /*******************************************************************************
988  *
989  * Function         BTA_GATTS_HandleValueIndication
990  *
991  * Description      This function is called to read a characteristics
992  *                  descriptor.
993  *
994  * Parameters       conn_id - connection identifier.
995  *                  attr_id - attribute ID to indicate.
996  *                  value - data to indicate.
997  *                  need_confirm - if this indication expects a confirmation or
998  *                                 not.
999  *
1000  * Returns          None
1001  *
1002  ******************************************************************************/
1003 void BTA_GATTS_HandleValueIndication(uint16_t conn_id, uint16_t attr_id,
1004                                      std::vector<uint8_t> value,
1005                                      bool need_confirm);
1006 
1007 /*******************************************************************************
1008  *
1009  * Function         BTA_GATTS_SendRsp
1010  *
1011  * Description      This function is called to send a response to a request.
1012  *
1013  * Parameters       conn_id - connection identifier.
1014  *                  trans_id - transaction ID.
1015  *                  status - response status
1016  *                  p_msg - response data.
1017  *
1018  * Returns          None
1019  *
1020  ******************************************************************************/
1021 void BTA_GATTS_SendRsp(uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status,
1022                        tGATTS_RSP* p_msg);
1023 
1024 /*******************************************************************************
1025  *
1026  * Function         BTA_GATTS_Open
1027  *
1028  * Description      Open a direct open connection or add a background auto
1029  *                  connection bd address
1030  *
1031  * Parameters       server_if: server interface.
1032  *                  remote_bda: remote device BD address.
1033  *                  addr_type: remote device address type
1034  *                  is_direct: direct connection or background auto connection
1035  *                  transport: transport to use in this connection
1036  *
1037  * Returns          void
1038  *
1039  ******************************************************************************/
1040 void BTA_GATTS_Open(tGATT_IF server_if, const RawAddress& remote_bda,
1041                     tBLE_ADDR_TYPE addr_type, bool is_direct,
1042                     tBT_TRANSPORT transport);
1043 
1044 /*******************************************************************************
1045  *
1046  * Function         BTA_GATTS_CancelOpen
1047  *
1048  * Description      Cancel a direct open connection or remove a background auto
1049  *                  connection bd address
1050  *
1051  * Parameters       server_if: server interface.
1052  *                  remote_bda: remote device BD address.
1053  *                  is_direct: direct connection or background auto connection
1054  *
1055  * Returns          void
1056  *
1057  ******************************************************************************/
1058 void BTA_GATTS_CancelOpen(tGATT_IF server_if, const RawAddress& remote_bda,
1059                           bool is_direct);
1060 
1061 /*******************************************************************************
1062  *
1063  * Function         BTA_GATTS_Close
1064  *
1065  * Description      Close a connection  a remote device.
1066  *
1067  * Parameters       conn_id: connectino ID to be closed.
1068  *
1069  * Returns          void
1070  *
1071  ******************************************************************************/
1072 void BTA_GATTS_Close(uint16_t conn_id);
1073 
1074 // Adds bonded device for GATT server tracking service changes
1075 void BTA_GATTS_InitBonded(void);
1076 
1077 namespace fmt {
1078 template <>
1079 struct formatter<tBTA_GATTC_EVT> : enum_formatter<tBTA_GATTC_EVT> {};
1080 }  // namespace fmt
1081 
1082 #endif /* BTA_GATT_API_H */
1083