OSDN Git Service

Replace BT_HDR => BT_HDR_RIGID
[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 <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 #define BTA_GATTC_CONN_MAX GATT_MAX_PHY_CHANNEL
79
80 #ifndef BTA_GATTC_CLCB_MAX
81 #define BTA_GATTC_CLCB_MAX GATT_CL_MAX_LCB
82 #endif
83
84 #define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE
85
86 /* internal strucutre for GATTC register API  */
87 typedef struct {
88   BT_HDR_RIGID hdr;
89   RawAddress remote_bda;
90   tGATT_IF client_if;
91   bool is_direct;
92   tBT_TRANSPORT transport;
93   uint8_t initiating_phys;
94   bool opportunistic;
95 } tBTA_GATTC_API_OPEN;
96
97 typedef tBTA_GATTC_API_OPEN tBTA_GATTC_API_CANCEL_OPEN;
98
99 typedef struct {
100   BT_HDR_RIGID hdr;
101   tGATT_AUTH_REQ auth_req;
102
103   // read by handle data
104   uint16_t handle;
105
106   // read by UUID data
107   bluetooth::Uuid uuid;
108   uint16_t s_handle;
109   uint16_t e_handle;
110
111   tBTA_GATTC_EVT cmpl_evt;
112   GATT_READ_OP_CB read_cb;
113   void* read_cb_data;
114 } tBTA_GATTC_API_READ;
115
116 typedef struct {
117   BT_HDR_RIGID hdr;
118   tGATT_AUTH_REQ auth_req;
119   uint16_t handle;
120   tGATT_WRITE_TYPE write_type;
121   uint16_t offset;
122   uint16_t len;
123   uint8_t* p_value;
124   GATT_WRITE_OP_CB write_cb;
125   void* write_cb_data;
126 } tBTA_GATTC_API_WRITE;
127
128 typedef struct {
129   BT_HDR_RIGID hdr;
130   bool is_execute;
131 } tBTA_GATTC_API_EXEC;
132
133 typedef struct {
134   BT_HDR_RIGID hdr;
135   uint16_t cid;
136 } tBTA_GATTC_API_CONFIRM;
137
138 typedef tGATT_CL_COMPLETE tBTA_GATTC_CMPL;
139
140 typedef struct {
141   BT_HDR_RIGID hdr;
142   uint8_t op_code;
143   tGATT_STATUS status;
144   tBTA_GATTC_CMPL* p_cmpl;
145 } tBTA_GATTC_OP_CMPL;
146
147 typedef struct {
148   BT_HDR_RIGID hdr;
149   bluetooth::Uuid* p_srvc_uuid;
150 } tBTA_GATTC_API_SEARCH;
151
152 typedef struct {
153   BT_HDR_RIGID hdr;
154   tGATT_AUTH_REQ auth_req;
155   uint8_t num_attr;
156   uint16_t handles[GATT_MAX_READ_MULTI_HANDLES];
157 } tBTA_GATTC_API_READ_MULTI;
158
159 typedef struct {
160   BT_HDR_RIGID hdr;
161   uint16_t mtu;
162   GATT_CONFIGURE_MTU_OP_CB mtu_cb;
163   void* mtu_cb_data;
164 } tBTA_GATTC_API_CFG_MTU;
165
166 typedef struct {
167   BT_HDR_RIGID 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_RIGID 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 } tBTA_GATTC_CLCB;
276
277 /* back ground connection tracking information */
278 #if GATT_MAX_APPS <= 8
279 typedef uint8_t tBTA_GATTC_CIF_MASK;
280 #elif GATT_MAX_APPS <= 16
281 typedef uint16_t tBTA_GATTC_CIF_MASK;
282 #elif GATT_MAX_APPS <= 32
283 typedef uint32_t tBTA_GATTC_CIF_MASK;
284 #endif
285
286 typedef struct {
287   bool in_use;
288   RawAddress remote_bda;
289   tBTA_GATTC_CIF_MASK cif_mask;
290
291 } tBTA_GATTC_BG_TCK;
292
293 typedef struct {
294   bool in_use;
295   RawAddress remote_bda;
296 } tBTA_GATTC_CONN;
297
298 enum {
299   BTA_GATTC_STATE_DISABLED,
300   BTA_GATTC_STATE_ENABLING,
301   BTA_GATTC_STATE_ENABLED,
302   BTA_GATTC_STATE_DISABLING
303 };
304
305 typedef struct {
306   uint8_t state;
307
308   tBTA_GATTC_CONN conn_track[BTA_GATTC_CONN_MAX];
309   tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX];
310   tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX];
311
312   tBTA_GATTC_CLCB clcb[BTA_GATTC_CLCB_MAX];
313   tBTA_GATTC_SERV known_server[BTA_GATTC_KNOWN_SR_MAX];
314 } tBTA_GATTC_CB;
315
316 /*****************************************************************************
317  *  Global data
318  ****************************************************************************/
319
320 /* GATTC control block */
321 extern tBTA_GATTC_CB bta_gattc_cb;
322
323 /*****************************************************************************
324  *  Function prototypes
325  ****************************************************************************/
326 extern bool bta_gattc_hdl_event(BT_HDR_RIGID* p_msg);
327 extern bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event,
328                                  tBTA_GATTC_DATA* p_data);
329
330 /* function processed outside SM */
331 extern void bta_gattc_disable();
332 extern void bta_gattc_register(const bluetooth::Uuid& app_uuid,
333                                tBTA_GATTC_CBACK* p_data,
334                                BtaAppRegisterCallback cb, bool eatt_support);
335 extern void bta_gattc_process_api_open(tBTA_GATTC_DATA* p_msg);
336 extern void bta_gattc_process_api_open_cancel(tBTA_GATTC_DATA* p_msg);
337 extern void bta_gattc_deregister(tBTA_GATTC_RCB* p_clreg);
338
339 /* function within state machine */
340 extern void bta_gattc_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
341 extern void bta_gattc_open_fail(tBTA_GATTC_CLCB* p_clcb,
342                                 tBTA_GATTC_DATA* p_data);
343 extern void bta_gattc_open_error(tBTA_GATTC_CLCB* p_clcb,
344                                  tBTA_GATTC_DATA* p_data);
345
346 extern void bta_gattc_cancel_open(tBTA_GATTC_CLCB* p_clcb,
347                                   tBTA_GATTC_DATA* p_data);
348 extern void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB* p_clcb,
349                                      tBTA_GATTC_DATA* p_data);
350 extern void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB* p_clcb,
351                                         tBTA_GATTC_DATA* p_data);
352
353 extern void bta_gattc_conn(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
354
355 extern void bta_gattc_close(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
356 extern void bta_gattc_close_fail(tBTA_GATTC_CLCB* p_clcb,
357                                  tBTA_GATTC_DATA* p_data);
358 extern void bta_gattc_disc_close(tBTA_GATTC_CLCB* p_clcb,
359                                  tBTA_GATTC_DATA* p_data);
360
361 extern void bta_gattc_start_discover(tBTA_GATTC_CLCB* p_clcb,
362                                      tBTA_GATTC_DATA* p_data);
363 extern void bta_gattc_start_discover_internal(tBTA_GATTC_CLCB* p_clcb);
364 extern void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB* p_clcb,
365                                 tBTA_GATTC_DATA* p_data);
366 extern void bta_gattc_read(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
367 extern void bta_gattc_write(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
368 extern void bta_gattc_op_cmpl(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
369 extern void bta_gattc_q_cmd(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
370 extern void bta_gattc_search(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
371 extern void bta_gattc_fail(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
372 extern void bta_gattc_confirm(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
373 extern void bta_gattc_execute(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
374 extern void bta_gattc_read_multi(tBTA_GATTC_CLCB* p_clcb,
375                                  tBTA_GATTC_DATA* p_data);
376 extern void bta_gattc_ci_open(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
377 extern void bta_gattc_ci_close(tBTA_GATTC_CLCB* p_clcb,
378                                tBTA_GATTC_DATA* p_data);
379 extern void bta_gattc_op_cmpl_during_discovery(tBTA_GATTC_CLCB* p_clcb,
380                                                tBTA_GATTC_DATA* p_data);
381 extern void bta_gattc_restart_discover(tBTA_GATTC_CLCB* p_clcb,
382                                        tBTA_GATTC_DATA* p_msg);
383 extern void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN* p_data,
384                                    tBTA_GATTC_RCB* p_clreg);
385 extern void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN* p_data);
386 extern void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg,
387                                       tGATT_STATUS status,
388                                       const RawAddress& remote_bda,
389                                       uint16_t conn_id, tBT_TRANSPORT transport,
390                                       uint16_t mtu);
391 extern void bta_gattc_process_api_refresh(const RawAddress& remote_bda);
392 extern void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
393 extern void bta_gattc_listen(tBTA_GATTC_DATA* p_msg);
394 extern void bta_gattc_broadcast(tBTA_GATTC_DATA* p_msg);
395
396 /* utility functions */
397 extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if,
398                                                    const RawAddress& remote_bda,
399                                                    tBT_TRANSPORT transport);
400 extern tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(uint16_t conn_id);
401 extern tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tGATT_IF client_if,
402                                              const RawAddress& remote_bda,
403                                              tBT_TRANSPORT transport);
404 extern void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb);
405 extern tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tGATT_IF client_if,
406                                                   const RawAddress& remote_bda,
407                                                   tBT_TRANSPORT transport);
408 extern tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if);
409 extern tBTA_GATTC_SERV* bta_gattc_find_srcb(const RawAddress& bda);
410 extern tBTA_GATTC_SERV* bta_gattc_srcb_alloc(const RawAddress& bda);
411 extern tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(uint16_t conn_id);
412 extern tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg);
413 extern tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg);
414
415 extern bool bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data);
416
417 extern bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg,
418                                            tBTA_GATTC_SERV* p_srcb,
419                                            tBTA_GATTC_NOTIFY* p_notify);
420 extern bool bta_gattc_mark_bg_conn(tGATT_IF client_if,
421                                    const RawAddress& remote_bda, bool add);
422 extern bool bta_gattc_check_bg_conn(tGATT_IF client_if,
423                                     const RawAddress& remote_bda, uint8_t role);
424 extern uint8_t bta_gattc_num_reg_app(void);
425 extern void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb,
426                                                uint16_t conn_id,
427                                                uint16_t start_handle,
428                                                uint16_t end_handle);
429 extern tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(const RawAddress& bda);
430 extern bool bta_gattc_is_robust_caching_enabled();
431
432 /* discovery functions */
433 extern void bta_gattc_disc_res_cback(uint16_t conn_id,
434                                      tGATT_DISC_TYPE disc_type,
435                                      tGATT_DISC_RES* p_data);
436 extern void bta_gattc_disc_cmpl_cback(uint16_t conn_id,
437                                       tGATT_DISC_TYPE disc_type,
438                                       tGATT_STATUS status);
439 extern tGATT_STATUS bta_gattc_discover_pri_service(uint16_t conn_id,
440                                                    tBTA_GATTC_SERV* p_server_cb,
441                                                    tGATT_DISC_TYPE disc_type);
442 extern void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb,
443                                      bluetooth::Uuid* p_uuid);
444 extern const std::list<gatt::Service>* bta_gattc_get_services(uint16_t conn_id);
445 extern const gatt::Service* bta_gattc_get_service_for_handle(uint16_t conn_id,
446                                                              uint16_t handle);
447 const gatt::Characteristic* bta_gattc_get_characteristic_srcb(
448     tBTA_GATTC_SERV* p_srcb, uint16_t handle);
449 extern const gatt::Service* bta_gattc_get_service_for_handle_srcb(
450     tBTA_GATTC_SERV* p_srcb, uint16_t handle);
451 extern const gatt::Characteristic* bta_gattc_get_characteristic(
452     uint16_t conn_id, uint16_t handle);
453 extern const gatt::Descriptor* bta_gattc_get_descriptor(uint16_t conn_id,
454                                                         uint16_t handle);
455 extern const gatt::Characteristic* bta_gattc_get_owning_characteristic(
456     uint16_t conn_id, uint16_t handle);
457 extern void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle,
458                                   uint16_t end_handle, btgatt_db_element_t** db,
459                                   int* count);
460 extern void bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb);
461 extern void bta_gattc_reset_discover_st(tBTA_GATTC_SERV* p_srcb,
462                                         tGATT_STATUS status);
463
464 extern tBTA_GATTC_CONN* bta_gattc_conn_alloc(const RawAddress& remote_bda);
465 extern tBTA_GATTC_CONN* bta_gattc_conn_find(const RawAddress& remote_bda);
466 extern tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(const RawAddress& remote_bda);
467 extern bool bta_gattc_conn_dealloc(const RawAddress& remote_bda);
468
469 extern bool bta_gattc_cache_load(tBTA_GATTC_SERV* p_srcb);
470 extern void bta_gattc_cache_reset(const RawAddress& server_bda);
471
472 extern bool bta_gattc_read_db_hash(tBTA_GATTC_CLCB* p_clcb);
473
474 #endif /* BTA_GATTC_INT_H */