1 /******************************************************************************
2  *
3  *  Copyright 2003-2012 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 private file for the file transfer client (FTC).
22  *
23  ******************************************************************************/
24 #ifndef BTA_GATTC_INT_H
25 #define BTA_GATTC_INT_H
26 
27 #include <bluetooth/log.h>
28 
29 #include <cstdint>
30 #include <deque>
31 
32 #include "bta/gatt/database.h"
33 #include "bta/gatt/database_builder.h"
34 #include "bta/include/bta_gatt_api.h"
35 #include "bta/sys/bta_sys.h"
36 #include "internal_include/bt_target.h"
37 #include "stack/include/bt_hdr.h"
38 #include "stack/include/gatt_api.h"
39 #include "types/bluetooth/uuid.h"
40 #include "types/bt_transport.h"
41 #include "types/raw_address.h"
42 
43 /*****************************************************************************
44  *  Constants and data types
45  ****************************************************************************/
46 enum {
47   BTA_GATTC_API_OPEN_EVT = BTA_SYS_EVT_START(BTA_ID_GATTC),
48   BTA_GATTC_INT_OPEN_FAIL_EVT,
49   BTA_GATTC_API_CANCEL_OPEN_EVT,
50   BTA_GATTC_INT_CANCEL_OPEN_OK_EVT,
51 
52   BTA_GATTC_API_READ_EVT,
53   BTA_GATTC_API_WRITE_EVT,
54   BTA_GATTC_API_EXEC_EVT,
55   BTA_GATTC_API_CFG_MTU_EVT,
56 
57   BTA_GATTC_API_CLOSE_EVT,
58 
59   BTA_GATTC_API_SEARCH_EVT,
60   BTA_GATTC_API_CONFIRM_EVT,
61   BTA_GATTC_API_READ_MULTI_EVT,
62 
63   BTA_GATTC_INT_CONN_EVT,
64   BTA_GATTC_INT_DISCOVER_EVT,
65   BTA_GATTC_DISCOVER_CMPL_EVT,
66   BTA_GATTC_OP_CMPL_EVT,
67   BTA_GATTC_INT_DISCONN_EVT
68 };
69 typedef uint16_t tBTA_GATTC_INT_EVT;
70 
71 #define BTA_GATTC_SERVICE_CHANGED_LEN 4
72 
73 /* Max client application GATTC can support */
74 #ifndef BTA_GATTC_CL_MAX
75 #define BTA_GATTC_CL_MAX 32
76 #endif
77 
78 /* max known devices GATTC can support in Bluetooth spec */
79 #ifndef BTA_GATTC_KNOWN_SR_MAX
80 #define BTA_GATTC_KNOWN_SR_MAX 255
81 #endif
82 
83 /* This represents number of gatt client control blocks per connection.
84  *  Because of that this value shall depends on the number of possible GATT
85  *  connections  GATT_MAX_PHY_CHANNEL
86  */
87 #ifndef BTA_GATTC_CLCB_MAX
88 #define BTA_GATTC_CLCB_MAX ((GATT_MAX_PHY_CHANNEL) * (BTA_GATTC_CL_MAX))
89 #endif
90 
91 #define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE
92 
93 typedef enum : uint8_t {
94   BTA_GATTC_SERV_IDLE = 0,
95   BTA_GATTC_SERV_LOAD,
96   BTA_GATTC_SERV_SAVE,
97   BTA_GATTC_SERV_DISC,
98   BTA_GATTC_SERV_DISC_ACT
99 } tBTA_GATTC_SERV_STATE;
100 
101 /* internal strucutre for GATTC register API  */
102 typedef struct {
103   BT_HDR_RIGID hdr;
104   RawAddress remote_bda;
105   tGATT_IF client_if;
106   tBTM_BLE_CONN_TYPE connection_type;
107   tBT_TRANSPORT transport;
108   uint8_t initiating_phys;
109   bool opportunistic;
110   tBT_DEVICE_TYPE remote_addr_type;
111 } tBTA_GATTC_API_OPEN;
112 
113 typedef struct {
114   BT_HDR_RIGID hdr;
115   RawAddress remote_bda;
116   tGATT_IF client_if;
117   bool is_direct;
118   tBT_TRANSPORT transport;
119   uint8_t initiating_phys;
120   bool opportunistic;
121 } tBTA_GATTC_API_CANCEL_OPEN;
122 
123 typedef struct {
124   BT_HDR_RIGID hdr;
125 
126   /* it is important that is_multi_read field stays at same position between
127    * tBTA_GATTC_API_READ and tBTA_GATTC_API_READ_MULTI, as it is read from
128    * parent union */
129   uint8_t is_multi_read;
130 
131   tGATT_AUTH_REQ auth_req;
132 
133   // read by handle data
134   uint16_t handle;
135 
136   // read by UUID data
137   bluetooth::Uuid uuid;
138   uint16_t s_handle;
139   uint16_t e_handle;
140 
141   tBTA_GATTC_EVT cmpl_evt;
142   GATT_READ_OP_CB read_cb;
143   void* read_cb_data;
144 } tBTA_GATTC_API_READ;
145 
146 typedef struct {
147   BT_HDR_RIGID hdr;
148   tGATT_AUTH_REQ auth_req;
149   uint16_t handle;
150   tGATT_WRITE_TYPE write_type;
151   uint16_t offset;
152   uint16_t len;
153   uint8_t* p_value;
154   GATT_WRITE_OP_CB write_cb;
155   void* write_cb_data;
156 } tBTA_GATTC_API_WRITE;
157 
158 typedef struct {
159   BT_HDR_RIGID hdr;
160   bool is_execute;
161 } tBTA_GATTC_API_EXEC;
162 
163 typedef struct {
164   BT_HDR_RIGID hdr;
165   uint16_t cid;
166 } tBTA_GATTC_API_CONFIRM;
167 
168 typedef struct {
169   BT_HDR_RIGID hdr;
170   tGATTC_OPTYPE op_code;
171   tGATT_STATUS status;
172   tGATT_CL_COMPLETE* p_cmpl;
173 } tBTA_GATTC_OP_CMPL;
174 
175 typedef struct {
176   BT_HDR_RIGID hdr;
177   bluetooth::Uuid* p_srvc_uuid;
178 } tBTA_GATTC_API_SEARCH;
179 
180 typedef struct {
181   BT_HDR_RIGID hdr;
182 
183   /* it is important that is_multi_read field stays at same position between
184    * tBTA_GATTC_API_READ and tBTA_GATTC_API_READ_MULTI, as it is read from
185    * parent union */
186   uint8_t is_multi_read;
187 
188   tGATT_AUTH_REQ auth_req;
189   tBTA_GATTC_MULTI handles;
190   uint8_t variable_len;
191   GATT_READ_MULTI_OP_CB read_cb;
192   void* read_cb_data;
193 } tBTA_GATTC_API_READ_MULTI;
194 
195 typedef struct {
196   BT_HDR_RIGID hdr;
197   uint16_t mtu;
198   GATT_CONFIGURE_MTU_OP_CB mtu_cb;
199   void* mtu_cb_data;
200 } tBTA_GATTC_API_CFG_MTU;
201 
202 typedef struct {
203   BT_HDR_RIGID hdr;
204   RawAddress remote_bda;
205   tGATT_IF client_if;
206   uint8_t role;
207   tBT_TRANSPORT transport;
208   tGATT_DISCONN_REASON reason;
209 } tBTA_GATTC_INT_CONN;
210 
211 typedef union {
212   BT_HDR_RIGID hdr;
213   tBTA_GATTC_API_OPEN api_conn;
214   tBTA_GATTC_API_CANCEL_OPEN api_cancel_conn;
215   tBTA_GATTC_API_READ api_read;
216   tBTA_GATTC_API_SEARCH api_search;
217   tBTA_GATTC_API_WRITE api_write;
218   tBTA_GATTC_API_CONFIRM api_confirm;
219   tBTA_GATTC_API_EXEC api_exec;
220   tBTA_GATTC_API_READ_MULTI api_read_multi;
221   tBTA_GATTC_API_CFG_MTU api_mtu;
222   tBTA_GATTC_OP_CMPL op_cmpl;
223   tBTA_GATTC_INT_CONN int_conn;
224 } tBTA_GATTC_DATA;
225 
226 typedef enum : uint8_t {
227   BTA_GATTC_IDLE_ST = 0, /* Idle  */
228   BTA_GATTC_W4_CONN_ST,  /* Wait for connection -  (optional) */
229   BTA_GATTC_CONN_ST,     /* connected state */
230   BTA_GATTC_DISCOVER_ST  /* discover is in progress */
231 } tBTA_GATTC_STATE;
232 
233 typedef struct {
234   bool in_use;
235   RawAddress server_bda;
236   bool connected;
237 
238   tBTA_GATTC_SERV_STATE state;
239 
240   gatt::Database gatt_database;
241   uint8_t update_count; /* indication received */
242   uint8_t num_clcb;     /* number of associated CLCB */
243 
244   gatt::DatabaseBuilder pending_discovery;
245 
246   /* used only during service discovery, when reading Extended Characteristic
247    * Properties */
248   bool read_multiple_not_supported;
249 
250   uint8_t srvc_hdl_chg; /* service handle change indication pending */
251   bool srvc_hdl_db_hash;   /* read db hash pending */
252   uint8_t srvc_disc_count; /* current discovery retry count */
253   uint16_t attr_index;  /* cahce NV saving/loading attribute index */
254 
255   uint16_t mtu;
256 
257   bool disc_blocked_waiting_on_version;
258   uint16_t blocked_conn_id;
259 } tBTA_GATTC_SERV;
260 
261 #ifndef BTA_GATTC_NOTIF_REG_MAX
262 #define BTA_GATTC_NOTIF_REG_MAX 64
263 #endif
264 
265 typedef struct {
266   bool in_use;
267   bool app_disconnected;
268   RawAddress remote_bda;
269   uint16_t handle;
270 } tBTA_GATTC_NOTIF_REG;
271 
272 typedef struct {
273   tBTA_GATTC_CBACK* p_cback;
274   bool in_use;
275   tGATT_IF client_if; /* client interface with BTE stack for this application */
276   uint8_t num_clcb; /* number of associated CLCB */
277   bool dereg_pending;
278   bluetooth::Uuid app_uuid;
279   tBTA_GATTC_NOTIF_REG notif_reg[BTA_GATTC_NOTIF_REG_MAX];
280 } tBTA_GATTC_RCB;
281 
282 /* client channel is a mapping between a BTA client(cl_id) and a remote BD
283  * address */
284 typedef struct {
285   uint16_t bta_conn_id; /* client channel ID, unique for clcb */
286   RawAddress bda;
287   tBT_TRANSPORT transport;  /* channel transport */
288   tBTA_GATTC_RCB* p_rcb;    /* pointer to the registration CB */
289   tBTA_GATTC_SERV* p_srcb;  /* server cache CB */
290   const tBTA_GATTC_DATA* p_q_cmd; /* command in queue waiting for execution */
291   std::deque<const tBTA_GATTC_DATA*> p_q_cmd_queue;
292 
293 // request during discover state
294 #define BTA_GATTC_DISCOVER_REQ_NONE 0
295 #define BTA_GATTC_DISCOVER_REQ_READ_EXT_PROP_DESC 1
296 #define BTA_GATTC_DISCOVER_REQ_READ_DB_HASH 2
297 #define BTA_GATTC_DISCOVER_REQ_READ_DB_HASH_FOR_SVC_CHG 3
298 
299   uint8_t request_during_discovery; /* request during discover state */
300 
301 #define BTA_GATTC_NO_SCHEDULE 0
302 #define BTA_GATTC_DISC_WAITING 0x01
303 #define BTA_GATTC_REQ_WAITING 0x10
304 
305   uint8_t auto_update; /* auto update is waiting */
306   bool disc_active;
307   bool in_use;
308   tBTA_GATTC_STATE state;
309   tGATT_STATUS status;
310 } tBTA_GATTC_CLCB;
311 
312 /* back ground connection tracking information */
313 #if GATT_MAX_APPS <= 8
314 typedef uint8_t tBTA_GATTC_CIF_MASK;
315 #elif GATT_MAX_APPS <= 16
316 typedef uint16_t tBTA_GATTC_CIF_MASK;
317 #elif GATT_MAX_APPS <= 32
318 typedef uint32_t tBTA_GATTC_CIF_MASK;
319 #endif
320 
321 typedef struct {
322   bool in_use;
323   RawAddress remote_bda;
324   tBTA_GATTC_CIF_MASK cif_mask;
325 
326 } tBTA_GATTC_BG_TCK;
327 
328 typedef struct {
329   bool in_use;
330   RawAddress remote_bda;
331 } tBTA_GATTC_CONN;
332 
333 typedef enum : uint8_t {
334   BTA_GATTC_STATE_DISABLED,
335   BTA_GATTC_STATE_ENABLING,
336   BTA_GATTC_STATE_ENABLED,
337   BTA_GATTC_STATE_DISABLING
338 } tBTA_GATTC_CB_STATE;
339 
340 typedef struct {
341   tBTA_GATTC_CB_STATE state;
342 
343   tBTA_GATTC_CONN conn_track[GATT_MAX_PHY_CHANNEL];
344   tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX];
345   tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX];
346 
347   tBTA_GATTC_CLCB clcb[BTA_GATTC_CLCB_MAX];
348   tBTA_GATTC_SERV known_server[BTA_GATTC_KNOWN_SR_MAX];
349 } tBTA_GATTC_CB;
350 
351 /*****************************************************************************
352  *  Global data
353  ****************************************************************************/
354 
355 /* GATTC control block */
356 extern tBTA_GATTC_CB bta_gattc_cb;
357 
358 /*****************************************************************************
359  *  Function prototypes
360  ****************************************************************************/
361 bool bta_gattc_hdl_event(const BT_HDR_RIGID* p_msg);
362 bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event,
363                           const tBTA_GATTC_DATA* p_data);
364 
365 /* function processed outside SM */
366 void bta_gattc_disable();
367 void bta_gattc_register(const bluetooth::Uuid& app_uuid,
368                         tBTA_GATTC_CBACK* p_data, BtaAppRegisterCallback cb,
369                         bool eatt_support);
370 void bta_gattc_process_api_open(const tBTA_GATTC_DATA* p_msg);
371 void bta_gattc_process_api_open_cancel(const tBTA_GATTC_DATA* p_msg);
372 void bta_gattc_deregister(tBTA_GATTC_RCB* p_clreg);
373 
374 /* function within state machine */
375 void bta_gattc_open(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
376 void bta_gattc_open_fail(tBTA_GATTC_CLCB* p_clcb,
377                          const tBTA_GATTC_DATA* p_data);
378 void bta_gattc_open_error(tBTA_GATTC_CLCB* p_clcb,
379                           const tBTA_GATTC_DATA* p_data);
380 
381 void bta_gattc_cancel_open(tBTA_GATTC_CLCB* p_clcb,
382                            const tBTA_GATTC_DATA* p_data);
383 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB* p_clcb,
384                               const tBTA_GATTC_DATA* p_data);
385 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB* p_clcb,
386                                  const tBTA_GATTC_DATA* p_data);
387 
388 void bta_gattc_conn(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
389 
390 void bta_gattc_close(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
391 void bta_gattc_close_fail(tBTA_GATTC_CLCB* p_clcb,
392                           const tBTA_GATTC_DATA* p_data);
393 void bta_gattc_disc_close(tBTA_GATTC_CLCB* p_clcb,
394                           const tBTA_GATTC_DATA* p_data);
395 
396 void bta_gattc_start_discover(tBTA_GATTC_CLCB* p_clcb,
397                               const tBTA_GATTC_DATA* p_data);
398 void bta_gattc_start_discover_internal(tBTA_GATTC_CLCB* p_clcb);
399 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB* p_clcb,
400                          const tBTA_GATTC_DATA* p_data);
401 void bta_gattc_read(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
402 void bta_gattc_write(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
403 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
404 void bta_gattc_q_cmd(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
405 void bta_gattc_search(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
406 void bta_gattc_fail(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
407 void bta_gattc_confirm(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
408 void bta_gattc_execute(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
409 void bta_gattc_read_multi(tBTA_GATTC_CLCB* p_clcb,
410                           const tBTA_GATTC_DATA* p_data);
411 void bta_gattc_ci_open(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
412 void bta_gattc_ci_close(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
413 void bta_gattc_op_cmpl_during_discovery(tBTA_GATTC_CLCB* p_clcb,
414                                         const tBTA_GATTC_DATA* p_data);
415 void bta_gattc_restart_discover(tBTA_GATTC_CLCB* p_clcb,
416                                 const tBTA_GATTC_DATA* p_msg);
417 void bta_gattc_cancel_bk_conn(const tBTA_GATTC_API_CANCEL_OPEN* p_data);
418 void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg, tGATT_STATUS status,
419                                const RawAddress& remote_bda, uint16_t conn_id,
420                                tBT_TRANSPORT transport, uint16_t mtu);
421 void bta_gattc_process_api_refresh(const RawAddress& remote_bda);
422 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB* p_clcb, const tBTA_GATTC_DATA* p_data);
423 void bta_gattc_listen(tBTA_GATTC_DATA* p_msg);
424 void bta_gattc_broadcast(tBTA_GATTC_DATA* p_msg);
425 
426 /* utility functions */
427 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if,
428                                             const RawAddress& remote_bda,
429                                             tBT_TRANSPORT transport);
430 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(uint16_t conn_id);
431 tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tGATT_IF client_if,
432                                       const RawAddress& remote_bda,
433                                       tBT_TRANSPORT transport);
434 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb);
435 void bta_gattc_server_disconnected(tBTA_GATTC_SERV* p_srcb);
436 tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tGATT_IF client_if,
437                                            const RawAddress& remote_bda,
438                                            tBT_TRANSPORT transport);
439 tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if);
440 tBTA_GATTC_SERV* bta_gattc_find_srcb(const RawAddress& bda);
441 tBTA_GATTC_SERV* bta_gattc_srcb_alloc(const RawAddress& bda);
442 tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(uint16_t conn_id);
443 tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg);
444 tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg);
445 
446 enum BtaEnqueuedResult_t {
447   ENQUEUED_READY_TO_SEND,
448   ENQUEUED_FOR_LATER,
449 };
450 
451 BtaEnqueuedResult_t bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb,
452                                       const tBTA_GATTC_DATA* p_data);
453 bool bta_gattc_is_data_queued(tBTA_GATTC_CLCB* p_clcb,
454                               const tBTA_GATTC_DATA* p_data);
455 void bta_gattc_continue(tBTA_GATTC_CLCB* p_clcb);
456 void bta_gattc_send_mtu_response(tBTA_GATTC_CLCB* p_clcb,
457                                  const tBTA_GATTC_DATA* p_data,
458                                  uint16_t current_mtu);
459 void bta_gattc_cmpl_sendmsg(uint16_t conn_id, tGATTC_OPTYPE op,
460                             tGATT_STATUS status, tGATT_CL_COMPLETE* p_data);
461 
462 bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg,
463                                     tBTA_GATTC_SERV* p_srcb,
464                                     tBTA_GATTC_NOTIFY* p_notify);
465 bool bta_gattc_mark_bg_conn(tGATT_IF client_if, const RawAddress& remote_bda,
466                             bool add);
467 bool bta_gattc_check_bg_conn(tGATT_IF client_if, const RawAddress& remote_bda,
468                              uint8_t role);
469 uint8_t bta_gattc_num_reg_app(void);
470 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb,
471                                         uint16_t conn_id, uint16_t start_handle,
472                                         uint16_t end_handle);
473 tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(const RawAddress& bda);
474 
475 /* discovery functions */
476 void bta_gattc_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
477                               tGATT_DISC_RES* p_data);
478 void bta_gattc_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type,
479                                tGATT_STATUS status);
480 tGATT_STATUS bta_gattc_discover_pri_service(uint16_t conn_id,
481                                             tBTA_GATTC_SERV* p_server_cb,
482                                             tGATT_DISC_TYPE disc_type);
483 void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, bluetooth::Uuid* p_uuid);
484 const std::list<gatt::Service>* bta_gattc_get_services(uint16_t conn_id);
485 const gatt::Service* bta_gattc_get_service_for_handle(uint16_t conn_id,
486                                                       uint16_t handle);
487 const gatt::Characteristic* bta_gattc_get_characteristic_srcb(
488     tBTA_GATTC_SERV* p_srcb, uint16_t handle);
489 const gatt::Service* bta_gattc_get_service_for_handle_srcb(
490     tBTA_GATTC_SERV* p_srcb, uint16_t handle);
491 const gatt::Characteristic* bta_gattc_get_characteristic(uint16_t conn_id,
492                                                          uint16_t handle);
493 const gatt::Descriptor* bta_gattc_get_descriptor(uint16_t conn_id,
494                                                  uint16_t handle);
495 const gatt::Characteristic* bta_gattc_get_owning_characteristic(
496     uint16_t conn_id, uint16_t handle);
497 void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle,
498                            uint16_t end_handle, btgatt_db_element_t** db,
499                            int* count);
500 void bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb);
501 
502 enum class RobustCachingSupport {
503   UNSUPPORTED,
504   SUPPORTED,
505   UNKNOWN,
506   W4_REMOTE_VERSION
507 };
508 RobustCachingSupport GetRobustCachingSupport(const tBTA_GATTC_CLCB* p_clcb,
509                                              const gatt::Database& db);
510 
511 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV* p_srcb, tGATT_STATUS status);
512 
513 tBTA_GATTC_CONN* bta_gattc_conn_alloc(const RawAddress& remote_bda);
514 tBTA_GATTC_CONN* bta_gattc_conn_find(const RawAddress& remote_bda);
515 tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(const RawAddress& remote_bda);
516 bool bta_gattc_conn_dealloc(const RawAddress& remote_bda);
517 
518 /* bta_gattc_cache */
519 bool bta_gattc_read_db_hash(tBTA_GATTC_CLCB* p_clcb, bool is_svc_chg);
520 
521 /* bta_gattc_db_storage */
522 gatt::Database bta_gattc_hash_load(const Octet16& hash);
523 bool bta_gattc_hash_write(const Octet16& hash, const gatt::Database& database);
524 gatt::Database bta_gattc_cache_load(const RawAddress& server_bda);
525 void bta_gattc_cache_write(const RawAddress& server_bda,
526                            const gatt::Database& database);
527 void bta_gattc_cache_link(const RawAddress& server_bda, const Octet16& hash);
528 void bta_gattc_cache_reset(const RawAddress& server_bda);
529 
bta_clcb_state_text(const tBTA_GATTC_STATE & state)530 inline std::string bta_clcb_state_text(const tBTA_GATTC_STATE& state) {
531   switch (state) {
532     CASE_RETURN_TEXT(BTA_GATTC_IDLE_ST);
533     CASE_RETURN_TEXT(BTA_GATTC_W4_CONN_ST);
534     CASE_RETURN_TEXT(BTA_GATTC_CONN_ST);
535     CASE_RETURN_TEXT(BTA_GATTC_DISCOVER_ST);
536     default:
537       return base::StringPrintf("UNKNOWN[%hhu]", state);
538   }
539 }
540 
bta_server_state_text(const tBTA_GATTC_SERV_STATE & state)541 inline std::string bta_server_state_text(const tBTA_GATTC_SERV_STATE& state) {
542   switch (state) {
543     CASE_RETURN_TEXT(BTA_GATTC_SERV_IDLE);
544     CASE_RETURN_TEXT(BTA_GATTC_SERV_LOAD);
545     CASE_RETURN_TEXT(BTA_GATTC_SERV_SAVE);
546     CASE_RETURN_TEXT(BTA_GATTC_SERV_DISC);
547     CASE_RETURN_TEXT(BTA_GATTC_SERV_DISC_ACT);
548     default:
549       return base::StringPrintf("UNKNOWN[%hhu]", state);
550   }
551 }
552 
bta_gattc_state_text(const tBTA_GATTC_CB_STATE & state)553 inline std::string bta_gattc_state_text(const tBTA_GATTC_CB_STATE& state) {
554   switch (state) {
555     CASE_RETURN_TEXT(BTA_GATTC_STATE_DISABLED);
556     CASE_RETURN_TEXT(BTA_GATTC_STATE_ENABLING);
557     CASE_RETURN_TEXT(BTA_GATTC_STATE_ENABLED);
558     CASE_RETURN_TEXT(BTA_GATTC_STATE_DISABLING);
559     default:
560       return base::StringPrintf("UNKNOWN[%hhu]", state);
561   }
562 }
563 
564 namespace fmt {
565 template <>
566 struct formatter<tBTA_GATTC_CB_STATE> : enum_formatter<tBTA_GATTC_CB_STATE> {};
567 template <>
568 struct formatter<tBTA_GATTC_SERV_STATE>
569     : enum_formatter<tBTA_GATTC_SERV_STATE> {};
570 template <>
571 struct formatter<tBTA_GATTC_STATE> : enum_formatter<tBTA_GATTC_STATE> {};
572 template <>
573 struct formatter<RobustCachingSupport> : enum_formatter<RobustCachingSupport> {
574 };
575 }  // namespace fmt
576 
577 #endif /* BTA_GATTC_INT_H */
578