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