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