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   tCONN_ID 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   tCONN_ID conn_id;
144   tGATT_STATUS status;
145   uint16_t handle;
146 } tBTA_GATTC_WRITE;
147 
148 typedef struct {
149   tCONN_ID conn_id;
150   tGATT_STATUS status;
151 } tBTA_GATTC_EXEC_CMPL;
152 
153 typedef struct {
154   tCONN_ID conn_id;
155   tGATT_STATUS status;
156 } tBTA_GATTC_SEARCH_CMPL;
157 
158 typedef struct {
159   tCONN_ID conn_id;
160   tBTA_GATT_ID service_uuid;
161 } tBTA_GATTC_SRVC_RES;
162 
163 typedef struct {
164   tCONN_ID conn_id;
165   tGATT_STATUS status;
166   uint16_t mtu;
167 } tBTA_GATTC_CFG_MTU;
168 
169 typedef struct {
170   tGATT_STATUS status;
171   tCONN_ID 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   tCONN_ID 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   tCONN_ID 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   tCONN_ID 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   tCONN_ID 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   tCONN_ID 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   tCONN_ID 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   tCONN_ID conn_id;
233 } tBTA_GATTC_SERVICE_CHANGED;
234 
235 typedef struct {
236   RawAddress remote_bda;
237 } tBTA_GATTC_SERVICE_DISCOVERY_DONE;
238 
239 typedef struct {
240   tGATT_IF server_if;
241   tCONN_ID conn_id;
242   uint16_t subrate_factor;
243   uint16_t latency;
244   uint16_t cont_num;
245   uint16_t timeout;
246   tGATT_STATUS status;
247 } tBTA_GATTC_SUBRATE_CHG;
248 
249 typedef union {
250   tGATT_STATUS status;
251 
252   tBTA_GATTC_SEARCH_CMPL search_cmpl; /* discovery complete */
253   tBTA_GATTC_SRVC_RES srvc_res;       /* discovery result */
254   tBTA_GATTC_REG reg_oper;            /* registration data */
255   tBTA_GATTC_OPEN open;
256   tBTA_GATTC_CLOSE close;
257   tBTA_GATTC_READ read;           /* read attribute/descriptor data */
258   tBTA_GATTC_WRITE write;         /* write complete data */
259   tBTA_GATTC_EXEC_CMPL exec_cmpl; /*  execute complete */
260   tBTA_GATTC_NOTIFY notify;       /* notification/indication event data */
261   tBTA_GATTC_ENC_CMPL_CB enc_cmpl;
262   tBTA_GATTC_CFG_MTU cfg_mtu; /* configure MTU operation */
263   tBTA_GATTC_CONGEST congest;
264   tBTA_GATTC_PHY_UPDATE phy_update;
265   tBTA_GATTC_CONN_UPDATE conn_update;
266   tBTA_GATTC_SERVICE_CHANGED service_changed;
267   tBTA_GATTC_SERVICE_DISCOVERY_DONE service_discovery_done;
268   tBTA_GATTC_SUBRATE_CHG subrate_chg;
269 } tBTA_GATTC;
270 
271 /* GATTC enable callback function */
272 typedef void(tBTA_GATTC_ENB_CBACK)(tGATT_STATUS status);
273 
274 /* Client callback function */
275 typedef void(tBTA_GATTC_CBACK)(tBTA_GATTC_EVT event, tBTA_GATTC* p_data);
276 
277 /* GATT Server Data Structure */
278 /* Server callback function events */
279 #define BTA_GATTS_REG_EVT 0
280 #define BTA_GATTS_READ_CHARACTERISTIC_EVT GATTS_REQ_TYPE_READ_CHARACTERISTIC   /* 1 */
281 #define BTA_GATTS_READ_DESCRIPTOR_EVT GATTS_REQ_TYPE_READ_DESCRIPTOR           /* 2 */
282 #define BTA_GATTS_WRITE_CHARACTERISTIC_EVT GATTS_REQ_TYPE_WRITE_CHARACTERISTIC /* 3 */
283 #define BTA_GATTS_WRITE_DESCRIPTOR_EVT GATTS_REQ_TYPE_WRITE_DESCRIPTOR         /* 4 */
284 #define BTA_GATTS_EXEC_WRITE_EVT GATTS_REQ_TYPE_WRITE_EXEC                     /* 5 */
285 #define BTA_GATTS_MTU_EVT GATTS_REQ_TYPE_MTU                                   /* 6 */
286 #define BTA_GATTS_CONF_EVT GATTS_REQ_TYPE_CONF                                 /* 7 */
287 #define BTA_GATTS_DEREG_EVT 8
288 #define BTA_GATTS_DELETE_EVT 11
289 #define BTA_GATTS_STOP_EVT 13
290 #define BTA_GATTS_CONNECT_EVT 14
291 #define BTA_GATTS_DISCONNECT_EVT 15
292 #define BTA_GATTS_OPEN_EVT 16
293 #define BTA_GATTS_CANCEL_OPEN_EVT 17
294 #define BTA_GATTS_CLOSE_EVT 18
295 #define BTA_GATTS_CONGEST_EVT 20
296 #define BTA_GATTS_PHY_UPDATE_EVT 21
297 #define BTA_GATTS_CONN_UPDATE_EVT 22
298 #define BTA_GATTS_SUBRATE_CHG_EVT 23
299 
300 typedef uint8_t tBTA_GATTS_EVT;
301 
gatt_server_event_text(const tBTA_GATTS_EVT & event)302 inline std::string gatt_server_event_text(const tBTA_GATTS_EVT& event) {
303   switch (event) {
304     CASE_RETURN_TEXT(BTA_GATTS_REG_EVT);
305     CASE_RETURN_TEXT(BTA_GATTS_READ_CHARACTERISTIC_EVT);
306     CASE_RETURN_TEXT(BTA_GATTS_READ_DESCRIPTOR_EVT);
307     CASE_RETURN_TEXT(BTA_GATTS_WRITE_CHARACTERISTIC_EVT);
308     CASE_RETURN_TEXT(BTA_GATTS_WRITE_DESCRIPTOR_EVT);
309     CASE_RETURN_TEXT(BTA_GATTS_EXEC_WRITE_EVT);
310     CASE_RETURN_TEXT(BTA_GATTS_MTU_EVT);
311     CASE_RETURN_TEXT(BTA_GATTS_CONF_EVT);
312     CASE_RETURN_TEXT(BTA_GATTS_DEREG_EVT);
313     CASE_RETURN_TEXT(BTA_GATTS_DELETE_EVT);
314     CASE_RETURN_TEXT(BTA_GATTS_STOP_EVT);
315     CASE_RETURN_TEXT(BTA_GATTS_CONNECT_EVT);
316     CASE_RETURN_TEXT(BTA_GATTS_DISCONNECT_EVT);
317     CASE_RETURN_TEXT(BTA_GATTS_OPEN_EVT);
318     CASE_RETURN_TEXT(BTA_GATTS_CANCEL_OPEN_EVT);
319     CASE_RETURN_TEXT(BTA_GATTS_CLOSE_EVT);
320     CASE_RETURN_TEXT(BTA_GATTS_CONGEST_EVT);
321     CASE_RETURN_TEXT(BTA_GATTS_PHY_UPDATE_EVT);
322     CASE_RETURN_TEXT(BTA_GATTS_CONN_UPDATE_EVT);
323     CASE_RETURN_TEXT(BTA_GATTS_SUBRATE_CHG_EVT);
324     default:
325       return base::StringPrintf("UNKNOWN[%hhu]", event);
326   }
327 }
328 
329 #define BTA_GATTS_INVALID_APP 0xff
330 
331 #define BTA_GATTS_INVALID_IF 0
332 
333 #ifndef BTA_GATTC_CHAR_DESCR_MAX
334 #define BTA_GATTC_CHAR_DESCR_MAX 7
335 #endif
336 
337 /***********************  NV callback Data Definitions   **********************
338  */
339 typedef struct {
340   bluetooth::Uuid app_uuid128;
341   bluetooth::Uuid svc_uuid;
342   uint16_t svc_inst;
343   uint16_t s_handle;
344   uint16_t e_handle;
345   bool is_primary; /* primary service or secondary */
346 } tBTA_GATTS_HNDL_RANGE;
347 
348 typedef struct {
349   tGATT_STATUS status;
350   RawAddress remote_bda;
351   uint32_t trans_id;
352   tCONN_ID conn_id;
353   tGATTS_DATA* p_data;
354 } tBTA_GATTS_REQ;
355 
356 typedef struct {
357   tGATT_IF server_if;
358   tGATT_STATUS status;
359   bluetooth::Uuid uuid;
360 } tBTA_GATTS_REG_OPER;
361 
362 typedef struct {
363   tGATT_IF server_if;
364   uint16_t service_id;
365   uint16_t svc_instance;
366   bool is_primary;
367   tGATT_STATUS status;
368   bluetooth::Uuid uuid;
369 } tBTA_GATTS_CREATE;
370 
371 typedef struct {
372   tGATT_IF server_if;
373   uint16_t service_id;
374   tGATT_STATUS status;
375 } tBTA_GATTS_SRVC_OPER;
376 
377 typedef struct {
378   tGATT_IF server_if;
379   RawAddress remote_bda;
380   tCONN_ID conn_id;
381   tBT_TRANSPORT transport;
382 } tBTA_GATTS_CONN;
383 
384 typedef struct {
385   tCONN_ID conn_id;
386   bool congested; /* report channel congestion indicator */
387 } tBTA_GATTS_CONGEST;
388 
389 typedef struct {
390   tCONN_ID conn_id;    /* connection ID */
391   tGATT_STATUS status; /* notification/indication status */
392 } tBTA_GATTS_CONF;
393 
394 typedef struct {
395   tGATT_IF server_if;
396   tCONN_ID conn_id;
397   uint8_t tx_phy;
398   uint8_t rx_phy;
399   tGATT_STATUS status;
400 } tBTA_GATTS_PHY_UPDATE;
401 
402 typedef struct {
403   tGATT_IF server_if;
404   tCONN_ID conn_id;
405   uint16_t interval;
406   uint16_t latency;
407   uint16_t timeout;
408   tGATT_STATUS status;
409 } tBTA_GATTS_CONN_UPDATE;
410 
411 typedef struct {
412   tGATT_IF server_if;
413   tCONN_ID conn_id;
414   uint16_t subrate_factor;
415   uint16_t latency;
416   uint16_t cont_num;
417   uint16_t timeout;
418   tGATT_STATUS status;
419 } tBTA_GATTS_SUBRATE_CHG;
420 
421 /* GATTS callback data */
422 typedef union {
423   tBTA_GATTS_REG_OPER reg_oper;
424   tBTA_GATTS_CREATE create;
425   tBTA_GATTS_SRVC_OPER srvc_oper;
426   tGATT_STATUS status; /* BTA_GATTS_LISTEN_EVT */
427   tBTA_GATTS_REQ req_data;
428   tBTA_GATTS_CONN conn;               /* BTA_GATTS_CONN_EVT */
429   tBTA_GATTS_CONGEST congest;         /* BTA_GATTS_CONGEST_EVT callback data */
430   tBTA_GATTS_CONF confirm;            /* BTA_GATTS_CONF_EVT callback data */
431   tBTA_GATTS_PHY_UPDATE phy_update;   /* BTA_GATTS_PHY_UPDATE_EVT callback data */
432   tBTA_GATTS_CONN_UPDATE conn_update; /* BTA_GATTS_CONN_UPDATE_EVT callback data */
433   tBTA_GATTS_SUBRATE_CHG subrate_chg; /* BTA_GATTS_SUBRATE_CHG_EVT */
434 } tBTA_GATTS;
435 
436 /* GATTS enable callback function */
437 typedef void(tBTA_GATTS_ENB_CBACK)(tGATT_STATUS status);
438 
439 /* Server callback function */
440 typedef void(tBTA_GATTS_CBACK)(tBTA_GATTS_EVT event, tBTA_GATTS* p_data);
441 
442 /*****************************************************************************
443  *  External Function Declarations
444  ****************************************************************************/
445 
446 /**************************
447  *  Client Functions
448  **************************/
449 
450 /*******************************************************************************
451  *
452  * Function         BTA_GATTC_Disable
453  *
454  * Description      This function is called to disable the GATTC module
455  *
456  * Parameters       None.
457  *
458  * Returns          None
459  *
460  ******************************************************************************/
461 void BTA_GATTC_Disable(void);
462 
463 using BtaAppRegisterCallback = 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, BtaAppRegisterCallback cb,
471                            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, tBLE_ADDR_TYPE addr_type,
503                     tBTM_BLE_CONN_TYPE connection_type, tBT_TRANSPORT transport, bool opportunistic,
504                     uint8_t initiating_phys, uint16_t preferred_mtu);
505 
506 /*******************************************************************************
507  *
508  * Function         BTA_GATTC_CancelOpen
509  *
510  * Description      Open a direct connection or add a background auto connection
511  *                  bd address
512  *
513  * Parameters       client_if: server interface.
514  *                  remote_bda: remote device BD address.
515  *                  is_direct: direct connection or background auto connection
516  *
517  * Returns          void
518  *
519  ******************************************************************************/
520 void BTA_GATTC_CancelOpen(tGATT_IF client_if, const RawAddress& remote_bda, bool is_direct);
521 
522 /*******************************************************************************
523  *
524  * Function         BTA_GATTC_Close
525  *
526  * Description      Close a connection to a GATT server.
527  *
528  * Parameters       conn_id: connectino ID to be closed.
529  *
530  * Returns          void
531  *
532  ******************************************************************************/
533 void BTA_GATTC_Close(tCONN_ID conn_id);
534 
535 /*******************************************************************************
536  *
537  * Function         BTA_GATTC_ServiceSearchAllRequest
538  *
539  * Description      This function is called to request a GATT service discovery
540  *                  of all services on a GATT server. This function report
541  *                  service search result by a callback event, and followed by a
542  *                  service search complete event.
543  *
544  * Parameters       conn_id: connection ID.
545  *
546  * Returns          None
547  *
548  ******************************************************************************/
549 void BTA_GATTC_ServiceSearchAllRequest(tCONN_ID conn_id);
550 
551 /*******************************************************************************
552  *
553  * Function         BTA_GATTC_ServiceSearchRequest
554  *
555  * Description      This function is called to request a GATT service discovery
556  *                  on a GATT server. This function report service search result
557  *                  by a callback event, and followed by a service search
558  *                  complete event.
559  *
560  * Parameters       conn_id: connection ID.
561  *                  p_srvc_uuid: a UUID of the service application is interested
562  *                               in.
563  * Returns          None
564  *
565  ******************************************************************************/
566 void BTA_GATTC_ServiceSearchRequest(tCONN_ID conn_id, bluetooth::Uuid p_srvc_uuid);
567 
568 /**
569  * This function is called to send "Find service by UUID" request. Used only for
570  * PTS tests.
571  */
572 void BTA_GATTC_DiscoverServiceByUuid(tCONN_ID conn_id, const bluetooth::Uuid& srvc_uuid);
573 
574 /*******************************************************************************
575  *
576  * Function         BTA_GATTC_GetServices
577  *
578  * Description      This function is called to find the services on the given
579  *                  server.
580  *
581  * Parameters       conn_id: connection ID which identify the server.
582  *
583  * Returns          returns list of gatt::Service or NULL.
584  *
585  ******************************************************************************/
586 const std::list<gatt::Service>* BTA_GATTC_GetServices(tCONN_ID conn_id);
587 
588 /*******************************************************************************
589  *
590  * Function         BTA_GATTC_GetCharacteristic
591  *
592  * Description      This function is called to find the characteristic on the
593  *                  given server.
594  *
595  * Parameters       conn_id: connection ID which identify the server.
596  *                  handle: characteristic handle
597  *
598  * Returns          returns pointer to gatt::Characteristic or NULL.
599  *
600  ******************************************************************************/
601 const gatt::Characteristic* BTA_GATTC_GetCharacteristic(tCONN_ID conn_id, uint16_t handle);
602 
603 /*******************************************************************************
604  *
605  * Function         BTA_GATTC_GetDescriptor
606  *
607  * Description      This function is called to find the characteristic on the
608  *                  given server.
609  *
610  * Parameters       conn_id: connection ID which identify the server.
611  *                  handle: descriptor handle
612  *
613  * Returns          returns pointer to gatt::Descriptor or NULL.
614  *
615  ******************************************************************************/
616 const gatt::Descriptor* BTA_GATTC_GetDescriptor(tCONN_ID conn_id, uint16_t handle);
617 
618 /* Return characteristic that owns descriptor with handle equal to |handle|, or
619  * NULL */
620 const gatt::Characteristic* BTA_GATTC_GetOwningCharacteristic(tCONN_ID conn_id, uint16_t handle);
621 
622 /* Return service that owns descriptor or characteristic with handle equal to
623  * |handle|, or NULL */
624 const gatt::Service* BTA_GATTC_GetOwningService(tCONN_ID conn_id, uint16_t handle);
625 
626 /*******************************************************************************
627  *
628  * Function         BTA_GATTC_GetGattDb
629  *
630  * Description      This function is called to get gatt db.
631  *
632  * Parameters       conn_id: connection ID which identify the server.
633  *                  db: output parameter which will contain gatt db copy.
634  *                      Caller is responsible for freeing it.
635  *                  count: number of elements in db.
636  *
637  ******************************************************************************/
638 void BTA_GATTC_GetGattDb(tCONN_ID conn_id, uint16_t start_handle, uint16_t end_handle,
639                          btgatt_db_element_t** db, int* count);
640 
641 typedef void (*GATT_READ_OP_CB)(tCONN_ID conn_id, tGATT_STATUS status, uint16_t handle,
642                                 uint16_t len, uint8_t* value, void* data);
643 typedef void (*GATT_WRITE_OP_CB)(tCONN_ID conn_id, tGATT_STATUS status, uint16_t handle,
644                                  uint16_t len, const uint8_t* value, void* data);
645 typedef void (*GATT_CONFIGURE_MTU_OP_CB)(tCONN_ID conn_id, tGATT_STATUS status, void* data);
646 typedef void (*GATT_READ_MULTI_OP_CB)(tCONN_ID conn_id, tGATT_STATUS status,
647                                       tBTA_GATTC_MULTI& handles, uint16_t len, uint8_t* value,
648                                       void* data);
649 /*******************************************************************************
650  *
651  * Function         BTA_GATTC_ReadCharacteristic
652  *
653  * Description      This function is called to read a characteristics value
654  *
655  * Parameters       conn_id - connectino ID.
656  *                  handle - characteritic handle to read.
657  *
658  * Returns          None
659  *
660  ******************************************************************************/
661 void BTA_GATTC_ReadCharacteristic(tCONN_ID conn_id, uint16_t handle, tGATT_AUTH_REQ auth_req,
662                                   GATT_READ_OP_CB callback, void* cb_data);
663 
664 /**
665  * This function is called to read a value of characteristic with uuid equal to
666  * |uuid|
667  */
668 void BTA_GATTC_ReadUsingCharUuid(tCONN_ID conn_id, const bluetooth::Uuid& uuid, uint16_t s_handle,
669                                  uint16_t e_handle, tGATT_AUTH_REQ auth_req,
670                                  GATT_READ_OP_CB callback, void* cb_data);
671 
672 /*******************************************************************************
673  *
674  * Function         BTA_GATTC_ReadCharDescr
675  *
676  * Description      This function is called to read a descriptor value.
677  *
678  * Parameters       conn_id - connection ID.
679  *                  handle - descriptor handle to read.
680  *
681  * Returns          None
682  *
683  ******************************************************************************/
684 void BTA_GATTC_ReadCharDescr(tCONN_ID conn_id, uint16_t handle, tGATT_AUTH_REQ auth_req,
685                              GATT_READ_OP_CB callback, void* cb_data);
686 
687 /*******************************************************************************
688  *
689  * Function         BTA_GATTC_WriteCharValue
690  *
691  * Description      This function is called to write characteristic value.
692  *
693  * Parameters       conn_id - connection ID.
694  *                  handle - characteristic handle to write.
695  *                  write_type - type of write.
696  *                  value - the value to be written.
697  *
698  * Returns          None
699  *
700  ******************************************************************************/
701 void BTA_GATTC_WriteCharValue(tCONN_ID conn_id, uint16_t handle, tGATT_WRITE_TYPE write_type,
702                               std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req,
703                               GATT_WRITE_OP_CB callback, void* cb_data);
704 
705 /*******************************************************************************
706  *
707  * Function         BTA_GATTC_WriteCharDescr
708  *
709  * Description      This function is called to write descriptor value.
710  *
711  * Parameters       conn_id - connection ID
712  *                  handle - descriptor handle to write.
713  *                  value - the value to be written.
714  *
715  * Returns          None
716  *
717  ******************************************************************************/
718 void BTA_GATTC_WriteCharDescr(tCONN_ID conn_id, uint16_t handle, std::vector<uint8_t> value,
719                               tGATT_AUTH_REQ auth_req, GATT_WRITE_OP_CB callback, void* cb_data);
720 
721 /*******************************************************************************
722  *
723  * Function         BTA_GATTC_SendIndConfirm
724  *
725  * Description      This function is called to send handle value confirmation.
726  *
727  * Parameters       conn_id - connection ID.
728  *                  cid - channel id
729  *
730  * Returns          None
731  *
732  ******************************************************************************/
733 void BTA_GATTC_SendIndConfirm(tCONN_ID conn_id, uint16_t cid);
734 
735 /*******************************************************************************
736  *
737  * Function         BTA_GATTC_RegisterForNotifications
738  *
739  * Description      This function is called to register for notification of a
740  *                  service.
741  *
742  * Parameters       client_if - client interface.
743  *                  remote_bda - target GATT server.
744  *                  handle - GATT characteristic handle.
745  *
746  * Returns          OK if registration succeed, otherwise failed.
747  *
748  ******************************************************************************/
749 tGATT_STATUS BTA_GATTC_RegisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
750                                                 uint16_t handle);
751 
752 /*******************************************************************************
753  *
754  * Function         BTA_GATTC_DeregisterForNotifications
755  *
756  * Description      This function is called to de-register for notification of a
757  *                  service.
758  *
759  * Parameters       client_if - client interface.
760  *                  remote_bda - target GATT server.
761  *                  handle - GATT characteristic handle.
762  *
763  * Returns          OK if deregistration succeed, otherwise failed.
764  *
765  ******************************************************************************/
766 tGATT_STATUS BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
767                                                   uint16_t handle);
768 
769 /*******************************************************************************
770  *
771  * Function         BTA_GATTC_PrepareWrite
772  *
773  * Description      This function is called to prepare write a characteristic
774  *                  value.
775  *
776  * Parameters       conn_id - connection ID.
777  *                  handle - GATT characteritic handle.
778  *                  offset - offset of the write value.
779  *                  value - the value to be written.
780  *
781  * Returns          None
782  *
783  ******************************************************************************/
784 void BTA_GATTC_PrepareWrite(tCONN_ID conn_id, uint16_t handle, uint16_t offset,
785                             std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req,
786                             GATT_WRITE_OP_CB callback, void* cb_data);
787 
788 /*******************************************************************************
789  *
790  * Function         BTA_GATTC_ExecuteWrite
791  *
792  * Description      This function is called to execute write a prepare write
793  *                  sequence.
794  *
795  * Parameters       conn_id - connection ID.
796  *                    is_execute - execute or cancel.
797  *
798  * Returns          None
799  *
800  ******************************************************************************/
801 void BTA_GATTC_ExecuteWrite(tCONN_ID conn_id, bool is_execute);
802 
803 /*******************************************************************************
804  *
805  * Function         BTA_GATTC_ReadMultiple
806  *
807  * Description      This function is called to read multiple characteristic or
808  *                  characteristic descriptors.
809  *
810  * Parameters       conn_id - connection ID.
811  *                  p_read_multi - read multiple parameters.
812  *                  variable_len - whether "read multi variable length" variant
813  *                                 shall be used.
814  *
815  * Returns          None
816  *
817  ******************************************************************************/
818 void BTA_GATTC_ReadMultiple(tCONN_ID conn_id, tBTA_GATTC_MULTI& p_read_multi, bool variable_len,
819                             tGATT_AUTH_REQ auth_req, GATT_READ_MULTI_OP_CB callback, void* cb_data);
820 
821 /*******************************************************************************
822  *
823  * Function         BTA_GATTC_Refresh
824  *
825  * Description      Refresh the server cache of the remote device
826  *
827  * Parameters       remote_bda: remote device BD address.
828  *
829  * Returns          void
830  *
831  ******************************************************************************/
832 void BTA_GATTC_Refresh(const RawAddress& remote_bda);
833 
834 /*******************************************************************************
835  *
836  * Function         BTA_GATTC_ConfigureMTU
837  *
838  * Description      Configure the MTU size in the GATT channel. This can be done
839  *                  only once per connection.
840  *
841  * Parameters       conn_id: connection ID.
842  *                  mtu: desired MTU size to use.
843  *
844  * Returns          void
845  *
846  ******************************************************************************/
847 void BTA_GATTC_ConfigureMTU(tCONN_ID conn_id, uint16_t mtu);
848 void BTA_GATTC_ConfigureMTU(tCONN_ID conn_id, uint16_t mtu, GATT_CONFIGURE_MTU_OP_CB callback,
849                             void* cb_data);
850 
851 /*******************************************************************************
852  *  BTA GATT Server API
853  ******************************************************************************/
854 
855 /*******************************************************************************
856  *
857  * Function         BTA_GATTS_Init
858  *
859  * Description      This function is called to initialize GATTS module
860  *
861  * Parameters       None
862  *
863  * Returns          None
864  *
865  ******************************************************************************/
866 void BTA_GATTS_Init();
867 
868 /*******************************************************************************
869  *
870  * Function         BTA_GATTS_Disable
871  *
872  * Description      This function is called to disable GATTS module
873  *
874  * Parameters       None.
875  *
876  * Returns          None
877  *
878  ******************************************************************************/
879 void BTA_GATTS_Disable(void);
880 
881 /*******************************************************************************
882  *
883  * Function         BTA_GATTS_AppRegister
884  *
885  * Description      This function is called to register application callbacks
886  *                    with BTA GATTS module.
887  *
888  * Parameters       p_app_uuid - application UUID
889  *                  p_cback - pointer to the application callback function.
890  *                  eatt_support: indicate eatt support.
891  *
892  * Returns          None
893  *
894  ******************************************************************************/
895 void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid, tBTA_GATTS_CBACK* p_cback,
896                            bool eatt_support);
897 
898 /*******************************************************************************
899  *
900  * Function         BTA_GATTS_AppDeregister
901  *
902  * Description      De-register with BTA GATT Server.
903  *
904  * Parameters       server_if: server interface
905  *
906  * Returns          void
907  *
908  ******************************************************************************/
909 void BTA_GATTS_AppDeregister(tGATT_IF server_if);
910 
911 /*******************************************************************************
912  *
913  * Function         BTA_GATTS_AddService
914  *
915  * Description      Add the given |service| and all included elements to the
916  *                  GATT database. a |BTA_GATTS_ADD_SRVC_EVT| is triggered to
917  *                  report the status and attribute handles.
918  *
919  * Parameters       server_if: server interface.
920  *                  service: pointer to vector describing service.
921  *
922  * Returns          Returns |GATT_SUCCESS| on success or |GATT_ERROR| if the
923  *                  service cannot be added.
924  *
925  ******************************************************************************/
926 typedef base::Callback<void(tGATT_STATUS status, int server_if,
927                             std::vector<btgatt_db_element_t> service)>
928         BTA_GATTS_AddServiceCb;
929 
930 void BTA_GATTS_AddService(tGATT_IF server_if, std::vector<btgatt_db_element_t> service,
931                           BTA_GATTS_AddServiceCb cb);
932 
933 /*******************************************************************************
934  *
935  * Function         BTA_GATTS_DeleteService
936  *
937  * Description      This function is called to delete a service. When this is
938  *                  done, a callback event BTA_GATTS_DELETE_EVT is report with
939  *                  the status.
940  *
941  * Parameters       service_id: service_id to be deleted.
942  *
943  * Returns          returns none.
944  *
945  ******************************************************************************/
946 void BTA_GATTS_DeleteService(uint16_t service_id);
947 
948 /*******************************************************************************
949  *
950  * Function         BTA_GATTS_StopService
951  *
952  * Description      This function is called to stop a service.
953  *
954  * Parameters       service_id - service to be topped.
955  *
956  * Returns          None
957  *
958  ******************************************************************************/
959 void BTA_GATTS_StopService(uint16_t service_id);
960 
961 /*******************************************************************************
962  *
963  * Function         BTA_GATTS_HandleValueIndication
964  *
965  * Description      This function is called to read a characteristics
966  *                  descriptor.
967  *
968  * Parameters       conn_id - connection identifier.
969  *                  attr_id - attribute ID to indicate.
970  *                  value - data to indicate.
971  *                  need_confirm - if this indication expects a confirmation or
972  *                                 not.
973  *
974  * Returns          None
975  *
976  ******************************************************************************/
977 void BTA_GATTS_HandleValueIndication(tCONN_ID conn_id, uint16_t attr_id, std::vector<uint8_t> value,
978                                      bool need_confirm);
979 
980 /*******************************************************************************
981  *
982  * Function         BTA_GATTS_SendRsp
983  *
984  * Description      This function is called to send a response to a request.
985  *
986  * Parameters       conn_id - connection identifier.
987  *                  trans_id - transaction ID.
988  *                  status - response status
989  *                  p_msg - response data.
990  *
991  * Returns          None
992  *
993  ******************************************************************************/
994 void BTA_GATTS_SendRsp(tCONN_ID conn_id, uint32_t trans_id, tGATT_STATUS status, tGATTS_RSP* p_msg);
995 
996 /*******************************************************************************
997  *
998  * Function         BTA_GATTS_Open
999  *
1000  * Description      Open a direct open connection or add a background auto
1001  *                  connection bd address
1002  *
1003  * Parameters       server_if: server interface.
1004  *                  remote_bda: remote device BD address.
1005  *                  addr_type: remote device address type
1006  *                  is_direct: direct connection or background auto connection
1007  *                  transport: transport to use in this connection
1008  *
1009  * Returns          void
1010  *
1011  ******************************************************************************/
1012 void BTA_GATTS_Open(tGATT_IF server_if, const RawAddress& remote_bda, tBLE_ADDR_TYPE addr_type,
1013                     bool is_direct, tBT_TRANSPORT transport);
1014 
1015 /*******************************************************************************
1016  *
1017  * Function         BTA_GATTS_CancelOpen
1018  *
1019  * Description      Cancel a direct open connection or remove a background auto
1020  *                  connection bd address
1021  *
1022  * Parameters       server_if: server interface.
1023  *                  remote_bda: remote device BD address.
1024  *                  is_direct: direct connection or background auto connection
1025  *
1026  * Returns          void
1027  *
1028  ******************************************************************************/
1029 void BTA_GATTS_CancelOpen(tGATT_IF server_if, const RawAddress& remote_bda, bool is_direct);
1030 
1031 /*******************************************************************************
1032  *
1033  * Function         BTA_GATTS_Close
1034  *
1035  * Description      Close a connection  a remote device.
1036  *
1037  * Parameters       conn_id: connectino ID to be closed.
1038  *
1039  * Returns          void
1040  *
1041  ******************************************************************************/
1042 void BTA_GATTS_Close(tCONN_ID conn_id);
1043 
1044 // Adds bonded device for GATT server tracking service changes
1045 void BTA_GATTS_InitBonded(void);
1046 
1047 namespace std {
1048 template <>
1049 struct formatter<tBTA_GATTC_EVT> : enum_formatter<tBTA_GATTC_EVT> {};
1050 }  // namespace std
1051 
1052 #endif /* BTA_GATT_API_H */
1053