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