OSDN Git Service

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