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