• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Keine Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

external/bluetooth/bluedroid


Commit MetaInfo

Revisione1202caae4920139ea0cfed5c51f5f76b2dc8bc4 (tree)
Zeit2013-08-14 09:40:44
AutorAndre Eisenbach <andre@broa...>
CommiterMatthew Xie

Log Message

LE: Add GATT disable functions

This patch adds required disable functions to the GATT sub-system to
properly unregister with the stack. Without the disable functions in
place, turning Bluetooth off with a GATT device connected may lead to
unexpected behaviour and cause GATT to fail on sub-sequent stack
restarts.

Change-Id: I7cb80e96109e2c09882991298d0487b506f5ffdd

Ändern Zusammenfassung

Diff

--- a/bta/dm/bta_dm_act.c
+++ b/bta/dm/bta_dm_act.c
@@ -102,6 +102,7 @@ static UINT8 bta_dm_ble_smp_cback (tBTM_LE_EVT event, BD_ADDR bda, tBTM_LE_EVT_D
102102 #endif
103103 static void bta_dm_ble_id_key_cback (UINT8 key_type, tBTM_BLE_LOCAL_KEYS *p_key);
104104 #if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE))
105+static void bta_dm_gattc_register(void);
105106 static void btm_dm_start_gatt_discovery ( BD_ADDR bd_addr);
106107 static void bta_dm_cancel_gatt_discovery(BD_ADDR bd_addr);
107108 static void bta_dm_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data);
@@ -381,10 +382,6 @@ static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status )
381382 {
382383 BTM_BleLoadLocalKeys(BTA_BLE_LOCAL_KEY_TYPE_ID, (tBTM_BLE_LOCAL_KEYS *)&id_key);
383384 }
384-#if ((defined BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE))
385- memset (&app_uuid.uu.uuid128, 0x87, LEN_UUID_128);
386- BTA_GATTC_AppRegister(&app_uuid, bta_dm_gattc_callback);
387-#endif
388385 #endif
389386
390387 BTM_SecRegister((tBTM_APPL_INFO*)&bta_security);
@@ -433,6 +430,11 @@ static void bta_dm_sys_hw_cback( tBTA_SYS_HW_EVT status )
433430 WBT_ExtAddPinCode();
434431 #endif
435432 #endif
433+#if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE)
434+ memset (&app_uuid.uu.uuid128, 0x87, LEN_UUID_128);
435+ bta_dm_gattc_register();
436+#endif
437+
436438 }
437439 else
438440 APPL_TRACE_DEBUG0(" --- ignored event");
@@ -1178,7 +1180,7 @@ void bta_dm_search_start (tBTA_DM_MSG *p_data)
11781180 {
11791181 tBTM_INQUIRY_CMPL result;
11801182
1181-#if BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE
1183+#if (BLE_INCLUDED == TRUE && BTA_GATT_INCLUDED == TRUE)
11821184 UINT16 len = (UINT16)(sizeof(tBT_UUID) * p_data->search.num_uuid);
11831185 #endif
11841186
@@ -4950,6 +4952,27 @@ void bta_dm_ble_observe (tBTA_DM_MSG *p_data)
49504952
49514953 /*******************************************************************************
49524954 **
4955+** Function bta_dm_gattc_register
4956+**
4957+** Description Register with GATTC in DM if BLE is needed.
4958+**
4959+**
4960+** Returns void
4961+**
4962+*******************************************************************************/
4963+static void bta_dm_gattc_register(void)
4964+{
4965+ tBT_UUID app_uuid = {LEN_UUID_128,{0}};
4966+
4967+ if (bta_dm_search_cb.client_if == BTA_GATTS_INVALID_IF)
4968+ {
4969+ memset (&app_uuid.uu.uuid128, 0x87, LEN_UUID_128);
4970+ BTA_GATTC_AppRegister(&app_uuid, bta_dm_gattc_callback);
4971+ }
4972+}
4973+
4974+/*******************************************************************************
4975+**
49534976 ** Function btm_dm_start_disc_gatt_services
49544977 **
49554978 ** Description This function starts a GATT service search request.
--- a/bta/gatt/bta_gattc_act.c
+++ b/bta/gatt/bta_gattc_act.c
@@ -25,9 +25,6 @@
2525
2626 #include "bt_target.h"
2727
28-#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
29-
30-
3128 #include "utl.h"
3229 #include "gki.h"
3330 #include "bd.h"
@@ -39,6 +36,8 @@
3936
4037 #include <string.h>
4138
39+#if BTA_GATT_INCLUDED && BLE_INCLUDED == TRUE
40+
4241 /*****************************************************************************
4342 ** Constants
4443 *****************************************************************************/
@@ -48,6 +47,8 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
4847 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
4948 tGATT_CL_COMPLETE *p_data);
5049
50+static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
51+
5152 static tGATT_CBACK bta_gattc_cl_cback =
5253 {
5354 bta_gattc_conn_cback,
@@ -84,6 +85,72 @@ static const char *bta_gattc_op_code_name[] =
8485
8586 /*******************************************************************************
8687 **
88+** Function bta_gattc_enable
89+**
90+** Description Enables GATTC module
91+**
92+**
93+** Returns void
94+**
95+*******************************************************************************/
96+static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
97+{
98+ APPL_TRACE_DEBUG0("bta_gattc_enable");
99+
100+ if (p_cb->state == BTA_GATTC_STATE_DISABLED)
101+ {
102+ /* initialize control block */
103+ memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
104+ p_cb->state = BTA_GATTC_STATE_ENABLED;
105+ }
106+ else
107+ {
108+ APPL_TRACE_DEBUG0("GATTC is arelady enabled");
109+ }
110+}
111+
112+
113+/*******************************************************************************
114+**
115+** Function bta_gattc_disable
116+**
117+** Description Disable GATTC module by cleaning up all active connections
118+** and deregister all application.
119+**
120+** Returns void
121+**
122+*******************************************************************************/
123+void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
124+{
125+ UINT8 i;
126+
127+ APPL_TRACE_DEBUG0("bta_gattc_disable");
128+
129+ if (p_cb->state != BTA_GATTC_STATE_ENABLED)
130+ {
131+ APPL_TRACE_ERROR0("not enabled or disable in pogress");
132+ return;
133+ }
134+
135+ for (i = 0; i <BTA_GATTC_CL_MAX; i ++)
136+ {
137+ if (p_cb->cl_rcb[i].in_use)
138+ {
139+ p_cb->state = BTA_GATTC_STATE_DISABLING;
140+ bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
141+ }
142+ }
143+
144+ /* no registered apps, indicate disable completed */
145+ if (p_cb->state != BTA_GATTC_STATE_DISABLING)
146+ {
147+ p_cb->state = BTA_GATTC_STATE_DISABLED;
148+ memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
149+ }
150+}
151+
152+/*******************************************************************************
153+**
87154 ** Function bta_gattc_register
88155 **
89156 ** Description Register a GATT client application with BTA.
@@ -99,6 +166,13 @@ void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
99166 tBTA_GATTC_INT_START_IF *p_buf;
100167 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
101168
169+ APPL_TRACE_DEBUG1("bta_gattc_register state %d",p_cb->state);
170+
171+ /* check if GATTC module is already enabled . Else enable */
172+ if (p_cb->state == BTA_GATTC_STATE_DISABLED)
173+ {
174+ bta_gattc_enable (p_cb);
175+ }
102176 /* todo need to check duplicate uuid */
103177 for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
104178 {
@@ -128,6 +202,9 @@ void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
128202 }
129203 else
130204 {
205+ GATT_Deregister(p_cb->cl_rcb[i].client_if);
206+
207+ status = BTA_GATT_NO_RESOURCES;
131208 memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB));
132209 }
133210 break;
@@ -167,99 +244,6 @@ void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
167244 }
168245 /*******************************************************************************
169246 **
170-** Function bta_gattc_deregister_cmpl
171-**
172-** Description De-Register a GATT client application with BTA completed.
173-**
174-** Returns void
175-**
176-*******************************************************************************/
177-void bta_gattc_int_deregister_cmpl(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_IF client_if)
178-{
179- tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
180- tBTA_GATTC cb_data;
181-
182-
183- APPL_TRACE_DEBUG1("bta_gattc_int_deregister_cmpl client_if=%d", client_if );
184-
185- GATT_Deregister(p_clreg->client_if);
186- memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
187-
188- cb_data.reg_oper.client_if = client_if;
189- cb_data.reg_oper.status = BTA_GATT_OK;
190-
191- if (p_cback)
192- /* callback with de-register event */
193- (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
194-}
195-
196-
197-/*******************************************************************************
198-**
199-** Function bta_gattc_deregister_cmpl
200-**
201-** Description De-Register a GATT client application with BTA completed.
202-**
203-** Returns void
204-**
205-*******************************************************************************/
206-void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_IF client_if)
207-{
208- tBTA_GATTC_INT_DEREG *p_buf;
209-
210- APPL_TRACE_DEBUG1("bta_gattc_deregister_cmpl client_if=%d", client_if );
211-
212- if ((p_buf = (tBTA_GATTC_INT_DEREG *) GKI_getbuf(sizeof(tBTA_GATTC_INT_DEREG))) != NULL)
213- {
214- p_buf->hdr.event = BTA_GATTC_INT_DEREG_EVT;
215- p_buf->client_if = client_if;
216- bta_sys_sendmsg(p_buf);
217- }
218- else
219- {
220- APPL_TRACE_ERROR1("bta_gattc_deregister_cmpl unable to allocate buffer to complete dereg=%d", client_if);
221- }
222-
223-}
224-/*******************************************************************************
225-**
226-** Function bta_gattc_deregister
227-**
228-** Description De-Register a GATT client application with BTA.
229-**
230-** Returns void
231-**
232-*******************************************************************************/
233-void bta_gattc_int_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
234-{
235-
236- tBTA_GATTC_IF client_if = p_data->int_dereg.client_if;
237- tBTA_GATTC_CBACK *p_cback;
238- tBTA_GATTC cb_data;
239- tBTA_GATTC_RCB *p_clreg;
240-
241-
242- APPL_TRACE_DEBUG1("bta_gattc_int_deregister_cmpl client_if=%d", client_if );
243-
244- if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
245- {
246- p_cback = p_clreg->p_cback;
247- GATT_Deregister(client_if);
248- memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
249- cb_data.reg_oper.client_if = client_if;
250- cb_data.reg_oper.status = BTA_GATT_OK;
251-
252- if (p_cback)
253- /* callback with de-register event */
254- (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
255- }
256- else
257- {
258- APPL_TRACE_ERROR1("bta_gattc_int_deregister Deregister Failed, unknown client_if: %d", p_data->int_dereg.client_if);
259- }
260-}
261-/*******************************************************************************
262-**
263247 ** Function bta_gattc_deregister
264248 **
265249 ** Description De-Register a GATT client application with BTA.
@@ -267,14 +251,30 @@ void bta_gattc_int_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
267251 ** Returns void
268252 **
269253 *******************************************************************************/
270-void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
254+void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg)
271255 {
272- tBTA_GATTC_RCB *p_clreg;
273256 UINT8 i;
274257 BT_HDR buf;
275258
276- if ((p_clreg = bta_gattc_cl_get_regcb(p_data->api_dereg.client_if)) != NULL)
259+ if (p_clreg != NULL)
277260 {
261+ /* remove bg connection associated with this rcb */
262+ for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++)
263+ {
264+ if (p_cb->bg_track[i].in_use)
265+ {
266+ if (p_cb->bg_track[i].cif_mask & (1 <<(p_clreg->client_if - 1)))
267+ {
268+ bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE);
269+ GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
270+ }
271+ if (p_cb->bg_track[i].cif_adv_mask & (1 <<(p_clreg->client_if - 1)))
272+ {
273+ bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
274+ }
275+ }
276+ }
277+
278278 if (p_clreg->num_clcb > 0)
279279 {
280280 /* close all CLCB related to this app */
@@ -291,11 +291,11 @@ void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
291291 }
292292 }
293293 else
294- bta_gattc_deregister_cmpl(p_clreg, p_clreg->client_if);
294+ bta_gattc_deregister_cmpl(p_clreg);
295295 }
296296 else
297297 {
298- APPL_TRACE_ERROR1("bta_gattc_deregister Deregister Failed, unknown client_if: %d", p_data->api_dereg.client_if);
298+ APPL_TRACE_ERROR0("bta_gattc_deregister Deregister Failedm unknown client cif");
299299 }
300300 }
301301 /*******************************************************************************
@@ -417,7 +417,7 @@ void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
417417 APPL_TRACE_ERROR0("Connection already opened. wrong state");
418418
419419 bta_gattc_send_open_cback(p_clcb->p_rcb,
420- BTA_GATT_ALREADY_OPEN,
420+ BTA_GATT_OK,
421421 p_clcb->bda,
422422 p_clcb->bta_conn_id);
423423 }
@@ -432,7 +432,11 @@ void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
432432 *******************************************************************************/
433433 void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
434434 {
435- bta_gattc_open_error(p_clcb, p_data);
435+ bta_gattc_send_open_cback(p_clcb->p_rcb,
436+ BTA_GATT_ERROR,
437+ p_clcb->bda,
438+ p_clcb->bta_conn_id);
439+
436440 /* open failure, remove clcb */
437441 bta_gattc_clcb_dealloc(p_clcb);
438442 }
@@ -702,22 +706,22 @@ void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
702706
703707 APPL_TRACE_DEBUG1("bta_gattc_close conn_id=%d",p_clcb->bta_conn_id);
704708
705- if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT)
706- p_clcb->status = GATT_Disconnect(p_clcb->bta_conn_id);
707-
708709 cb_data.close.client_if = p_clcb->p_rcb->client_if;
709710 cb_data.close.conn_id = p_clcb->bta_conn_id;
710- cb_data.close.status = p_clcb->status;
711711 cb_data.close.reason = p_clcb->reason;
712+ cb_data.close.status = p_clcb->status;
712713 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
713714
714715 bta_gattc_clcb_dealloc(p_clcb);
715716
717+ if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT)
718+ cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
719+
716720 ( * p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data);
717721
718722 if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending)
719723 {
720- bta_gattc_deregister_cmpl(p_clreg, p_clreg->client_if);
724+ bta_gattc_deregister_cmpl(p_clreg);
721725 }
722726 }
723727 /*******************************************************************************
@@ -1494,7 +1498,7 @@ void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
14941498 {
14951499 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
14961500 p_clcb->p_srcb->attr_index = 0;
1497- /* cache open failure, start discovery */
1501+ /* cache load failure, start discovery */
14981502 bta_gattc_start_discover(p_clcb, NULL);
14991503 }
15001504 }
@@ -1535,6 +1539,40 @@ void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
15351539 APPL_TRACE_ERROR1("operation not supported at current state [%d]", p_clcb->state);
15361540 }
15371541 }
1542+
1543+/*******************************************************************************
1544+**
1545+** Function bta_gattc_deregister_cmpl
1546+**
1547+** Description De-Register a GATT client application with BTA completed.
1548+**
1549+** Returns void
1550+**
1551+*******************************************************************************/
1552+static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1553+{
1554+ tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
1555+ tBTA_GATTC_IF client_if = p_clreg->client_if;
1556+ tBTA_GATTC cb_data;
1557+ tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
1558+
1559+ memset(&cb_data, 0, sizeof(tBTA_GATTC));
1560+
1561+ GATT_Deregister(p_clreg->client_if);
1562+ memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1563+
1564+ cb_data.reg_oper.client_if = client_if;
1565+ cb_data.reg_oper.status = BTA_GATT_OK;
1566+
1567+ if (p_cback)
1568+ /* callback with de-register event */
1569+ (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
1570+
1571+ if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1572+ {
1573+ p_cb->state = BTA_GATTC_STATE_DISABLED;
1574+ }
1575+}
15381576 /*******************************************************************************
15391577 **
15401578 ** Function bta_gattc_conn_cback
@@ -1550,25 +1588,21 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
15501588 {
15511589 BT_HDR *p_buf;
15521590 tBTA_GATTC_CLCB *p_clcb = NULL;
1553-#if BLE_INCLUDED == TRUE
15541591 UINT8 role ;
1555-#endif
1592+
15561593 APPL_TRACE_DEBUG4("bta_gattc_conn_cback: cif = %d connected = %d conn_id = %d reaosn = 0x%04x",
15571594 gattc_if, connected, conn_id, reason);
15581595
15591596 if (connected)
15601597 {
1561-#if BLE_INCLUDED == TRUE
15621598 role = L2CA_GetBleConnRole(bda);
15631599
15641600 if (role == HCI_ROLE_SLAVE)
15651601 bta_gattc_conn_find_alloc(bda);
1566-#endif
15671602
15681603 /* outgoing connection : locate a logic channel */
15691604 if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda)) == NULL)
15701605 {
1571-#if BLE_INCLUDED == TRUE
15721606 /* for a background connection or listening connection */
15731607 if (/* L2CA_GetBleConnRole(bda)== HCI_ROLE_MASTER && */
15741608 bta_gattc_check_bg_conn(gattc_if, bda, role))
@@ -1576,7 +1610,6 @@ static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
15761610 /* allocate a new channel */
15771611 p_clcb = bta_gattc_clcb_alloc(gattc_if, bda);
15781612 }
1579-#endif
15801613 }
15811614 if (p_clcb != NULL)
15821615 {
@@ -1883,7 +1916,6 @@ static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS
18831916 return;
18841917 }
18851918
1886-
18871919 if ((p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len)) != NULL)
18881920 {
18891921 memset(p_buf, 0, len);
--- a/bta/gatt/bta_gattc_api.c
+++ b/bta/gatt/bta_gattc_api.c
@@ -32,38 +32,44 @@
3232 #include "bta_gatt_api.h"
3333 #include "bta_gattc_int.h"
3434
35-
36-/*****************************************************************************
37-** Externs
38-*****************************************************************************/
39-#if BTA_DYNAMIC_MEMORY == FALSE
40-extern tBTA_GATTC_CB bta_gattc_cb;
41-#endif
42-
4335 /*****************************************************************************
4436 ** Constants
4537 *****************************************************************************/
4638
47-static const tBTA_SYS_REG bta_gatt_reg =
39+static const tBTA_SYS_REG bta_gattc_reg =
4840 {
4941 bta_gattc_hdl_event,
50- NULL /* need a disable functino to be called when BT is disabled */
42+ BTA_GATTC_Disable
5143 };
5244
45+
5346 /*******************************************************************************
5447 **
55-** Function BTA_GATTC_Init
48+** Function BTA_GATTC_Disable
5649 **
57-** Description This function is called to initalize GATTC module
50+** Description This function is called to disable GATTC module
5851 **
59-** Parameters None
52+** Parameters None.
6053 **
6154 ** Returns None
6255 **
6356 *******************************************************************************/
64-void BTA_GATTC_Init()
57+void BTA_GATTC_Disable(void)
6558 {
66- memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
59+ BT_HDR *p_buf;
60+
61+ if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
62+ {
63+ APPL_TRACE_WARNING0("GATTC Module not enabled/already disabled");
64+ return;
65+ }
66+ if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
67+ {
68+ p_buf->event = BTA_GATTC_API_DISABLE_EVT;
69+ bta_sys_sendmsg(p_buf);
70+ }
71+ bta_sys_deregister(BTA_ID_GATTC);
72+
6773 }
6874
6975 /*******************************************************************************
@@ -83,10 +89,12 @@ void BTA_GATTC_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTC_CBACK *p_client_cb)
8389 {
8490 tBTA_GATTC_API_REG *p_buf;
8591
86- /* register with BTA system manager */
87- GKI_sched_lock();
88- bta_sys_register(BTA_ID_GATTC, &bta_gatt_reg);
89- GKI_sched_unlock();
92+ if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
93+ {
94+ GKI_sched_lock();
95+ bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
96+ GKI_sched_unlock();
97+ }
9098
9199 if ((p_buf = (tBTA_GATTC_API_REG *) GKI_getbuf(sizeof(tBTA_GATTC_API_REG))) != NULL)
92100 {
--- a/bta/gatt/bta_gattc_int.h
+++ b/bta/gatt/bta_gattc_int.h
@@ -68,7 +68,7 @@ enum
6868 BTA_GATTC_INT_START_IF_EVT,
6969 BTA_GATTC_API_REG_EVT,
7070 BTA_GATTC_API_DEREG_EVT,
71- BTA_GATTC_INT_DEREG_EVT
71+ BTA_GATTC_API_DISABLE_EVT
7272
7373 };
7474 typedef UINT16 tBTA_GATTC_INT_EVT;
@@ -91,6 +91,7 @@ typedef UINT16 tBTA_GATTC_INT_EVT;
9191
9292 #define BTA_GATTC_WRITE_PREPARE GATT_WRITE_PREPARE
9393
94+
9495 /* internal strucutre for GATTC register API */
9596 typedef struct
9697 {
@@ -363,8 +364,18 @@ typedef struct
363364 BD_ADDR remote_bda;
364365 }tBTA_GATTC_CONN;
365366
367+enum
368+{
369+ BTA_GATTC_STATE_DISABLED,
370+ BTA_GATTC_STATE_ENABLING,
371+ BTA_GATTC_STATE_ENABLED,
372+ BTA_GATTC_STATE_DISABLING
373+};
374+
366375 typedef struct
367376 {
377+ UINT8 state;
378+
368379 tBTA_GATTC_CONN conn_track[BTA_GATTC_CONN_MAX];
369380 tBTA_GATTC_BG_TCK bg_track[BTA_GATTC_KNOWN_SR_MAX];
370381 tBTA_GATTC_RCB cl_rcb[BTA_GATTC_CL_MAX];
@@ -395,12 +406,12 @@ extern BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg);
395406 extern void bta_gattc_sm_execute(tBTA_GATTC_CLCB *p_clcb, UINT16 event, tBTA_GATTC_DATA *p_data);
396407
397408 /* function processed outside SM */
409+extern void bta_gattc_disable(tBTA_GATTC_CB *p_cb);
398410 extern void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
399411 extern void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
400412 extern void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
401413 extern void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg);
402-extern void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
403-extern void bta_gattc_int_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data);
414+extern void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg);
404415
405416 /* function within state machine */
406417 extern void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
--- a/bta/gatt/bta_gattc_main.c
+++ b/bta/gatt/bta_gattc_main.c
@@ -346,12 +346,16 @@ BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg)
346346 {
347347 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
348348 tBTA_GATTC_CLCB *p_clcb = NULL;
349-
349+ tBTA_GATTC_RCB *p_clreg;
350350 #if BTA_GATT_DEBUG == TRUE
351351 APPL_TRACE_DEBUG1("bta_gattc_hdl_event: Event [%s]", gattc_evt_code(p_msg->event));
352352 #endif
353353 switch (p_msg->event)
354354 {
355+ case BTA_GATTC_API_DISABLE_EVT:
356+ bta_gattc_disable(p_cb);
357+ break;
358+
355359 case BTA_GATTC_API_REG_EVT:
356360 bta_gattc_register(p_cb, (tBTA_GATTC_DATA *) p_msg);
357361 break;
@@ -361,11 +365,8 @@ BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg)
361365 break;
362366
363367 case BTA_GATTC_API_DEREG_EVT:
364- bta_gattc_deregister(p_cb, (tBTA_GATTC_DATA *) p_msg);
365- break;
366-
367- case BTA_GATTC_INT_DEREG_EVT:
368- bta_gattc_int_deregister(p_cb, (tBTA_GATTC_DATA *) p_msg);
368+ p_clreg = bta_gattc_cl_get_regcb(((tBTA_GATTC_DATA *)p_msg)->api_dereg.client_if);
369+ bta_gattc_deregister(p_cb, p_clreg);
369370 break;
370371
371372 case BTA_GATTC_API_OPEN_EVT:
@@ -465,6 +466,10 @@ static char *gattc_evt_code(tBTA_GATTC_INT_EVT evt_code)
465466 return "BTA_GATTC_API_DEREG_EVT";
466467 case BTA_GATTC_API_REFRESH_EVT:
467468 return "BTA_GATTC_API_REFRESH_EVT";
469+ case BTA_GATTC_API_DISABLE_EVT:
470+ return "BTA_GATTC_API_DISABLE_EVT";
471+ case BTA_GATTC_API_ENABLE_EVT:
472+ return "BTA_GATTC_API_ENABLE_EVT";
468473 default:
469474 return "unknown GATTC event code";
470475 }
--- a/bta/gatt/bta_gatts_act.c
+++ b/bta/gatt/bta_gatts_act.c
@@ -105,24 +105,66 @@ void bta_gatts_enable(tBTA_GATTS_CB *p_cb)
105105 {
106106 UINT8 index=0;
107107 tBTA_GATTS_HNDL_RANGE handle_range;
108+ tBTA_GATT_STATUS status = BTA_GATT_OK;
108109
109- p_cb->enabled = TRUE;
110-
111- APPL_TRACE_DEBUG0("bta_gatts_enable");
112- while ( bta_gatts_co_load_handle_range(index, &handle_range))
110+ if (p_cb->enabled)
113111 {
114- GATTS_AddHandleRange((tGATTS_HNDL_RANGE *)&handle_range);
115- memset(&handle_range, 0, sizeof(tGATTS_HNDL_RANGE));
116- index++;
112+ APPL_TRACE_DEBUG0("GATTS already enabled.");
117113 }
114+ else
115+ {
116+ memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
118117
119- APPL_TRACE_DEBUG1("bta_gatts_enable: num of handle range added=%d", index);
118+ p_cb->enabled = TRUE;
120119
121- if (!GATTS_NVRegister(&bta_gatts_nv_cback))
120+ while ( bta_gatts_co_load_handle_range(index, &handle_range))
121+ {
122+ GATTS_AddHandleRange((tGATTS_HNDL_RANGE *)&handle_range);
123+ memset(&handle_range, 0, sizeof(tGATTS_HNDL_RANGE));
124+ index++;
125+ }
126+
127+ APPL_TRACE_DEBUG1("bta_gatts_enable: num of handle range added=%d", index);
128+
129+ if (!GATTS_NVRegister(&bta_gatts_nv_cback))
130+ {
131+ APPL_TRACE_ERROR0("BTA GATTS NV register failed.");
132+ status = BTA_GATT_ERROR;
133+ }
134+ }
135+}
136+
137+/*******************************************************************************
138+**
139+** Function bta_gatts_api_disable
140+**
141+** Description disable BTA GATTS module.
142+**
143+** Returns none.
144+**
145+*******************************************************************************/
146+void bta_gatts_api_disable(tBTA_GATTS_CB *p_cb)
147+{
148+ UINT8 i;
149+ tBTA_GATT_STATUS status = BTA_GATT_OK;
150+
151+ if (p_cb->enabled)
152+ {
153+ for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
154+ {
155+ if (p_cb->rcb[i].in_use)
156+ {
157+ GATT_Deregister(p_cb->rcb[i].gatt_if);
158+ }
159+ }
160+ memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
161+ }
162+ else
122163 {
123- APPL_TRACE_ERROR0("BTA GATTS NV register failed.");
164+ APPL_TRACE_ERROR0("GATTS not enabled");
124165 }
125166 }
167+
126168 /*******************************************************************************
127169 **
128170 ** Function bta_gatts_register
@@ -139,9 +181,10 @@ void bta_gatts_register(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
139181 tBTA_GATT_STATUS status = BTA_GATT_OK;
140182 UINT8 i, first_unuse = 0xff;
141183
142- if (!p_cb->enabled)
184+ if (p_cb->enabled == FALSE)
185+ {
143186 bta_gatts_enable(p_cb);
144-
187+ }
145188
146189 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++)
147190 {
--- a/bta/gatt/bta_gatts_api.c
+++ b/bta/gatt/bta_gatts_api.c
@@ -33,36 +33,43 @@
3333 #include "bta_gatts_int.h"
3434
3535 /*****************************************************************************
36-** Externs
37-*****************************************************************************/
38-#if BTA_DYNAMIC_MEMORY == FALSE
39-extern tBTA_GATTS_CB bta_gatts_cb;
40-#endif
41-
42-/*****************************************************************************
4336 ** Constants
4437 *****************************************************************************/
4538
4639 static const tBTA_SYS_REG bta_gatts_reg =
4740 {
4841 bta_gatts_hdl_event,
49- NULL /* need a disable functino to be called when BT is disabled */
42+ BTA_GATTS_Disable
5043 };
5144
5245 /*******************************************************************************
5346 **
54-** Function BTA_GATTS_Init
47+** Function BTA_GATTS_Disable
5548 **
56-** Description This function is called to initalize GATTS module
49+** Description This function is called to disable GATTS module
5750 **
58-** Parameters None
51+** Parameters None.
5952 **
6053 ** Returns None
6154 **
6255 *******************************************************************************/
63-void BTA_GATTS_Init()
56+void BTA_GATTS_Disable(void)
6457 {
65- memset(&bta_gatts_cb, 0, sizeof(tBTA_GATTS_CB));
58+ BT_HDR *p_buf;
59+
60+ if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
61+ {
62+ APPL_TRACE_WARNING0("GATTS Module not enabled/already disabled");
63+ return;
64+ }
65+
66+ if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
67+ {
68+ p_buf->event = BTA_GATTS_API_DISABLE_EVT;
69+ bta_sys_sendmsg(p_buf);
70+ }
71+ bta_sys_deregister(BTA_ID_GATTS);
72+
6673 }
6774
6875 /*******************************************************************************
@@ -83,12 +90,12 @@ void BTA_GATTS_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTS_CBACK *p_cback)
8390 tBTA_GATTS_API_REG *p_buf;
8491
8592 /* register with BTA system manager */
86- GKI_sched_lock();
87- if (!bta_gatts_cb.enabled)
88- {
93+ if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
94+ {
95+ GKI_sched_lock();
8996 bta_sys_register(BTA_ID_GATTS, &bta_gatts_reg);
97+ GKI_sched_unlock();
9098 }
91- GKI_sched_unlock();
9299
93100 if ((p_buf = (tBTA_GATTS_API_REG *) GKI_getbuf(sizeof(tBTA_GATTS_API_REG))) != NULL)
94101 {
--- a/bta/gatt/bta_gatts_int.h
+++ b/bta/gatt/bta_gatts_int.h
@@ -52,8 +52,8 @@ enum
5252 BTA_GATTS_API_OPEN_EVT,
5353 BTA_GATTS_API_CANCEL_OPEN_EVT,
5454 BTA_GATTS_API_CLOSE_EVT,
55- BTA_GATTS_API_LISTEN_EVT
56-
55+ BTA_GATTS_API_LISTEN_EVT,
56+ BTA_GATTS_API_DISABLE_EVT
5757 };
5858 typedef UINT16 tBTA_GATTS_INT_EVT;
5959
@@ -224,6 +224,8 @@ extern tBTA_GATTS_CB *bta_gatts_cb_ptr;
224224 *****************************************************************************/
225225 extern BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg);
226226
227+extern void bta_gatts_api_disable(tBTA_GATTS_CB *p_cb);
228+extern void bta_gatts_api_enable(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_data);
227229 extern void bta_gatts_register(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
228230 extern void bta_gatts_start_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
229231 extern void bta_gatts_deregister(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg);
--- a/bta/gatt/bta_gatts_main.c
+++ b/bta/gatt/bta_gatts_main.c
@@ -67,6 +67,10 @@ BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg)
6767
6868 switch (p_msg->event)
6969 {
70+ case BTA_GATTS_API_DISABLE_EVT:
71+ bta_gatts_api_disable(p_cb);
72+ break;
73+
7074 case BTA_GATTS_API_REG_EVT:
7175 bta_gatts_register(p_cb, (tBTA_GATTS_DATA *) p_msg);
7276 break;
--- a/bta/include/bta_gatt_api.h
+++ b/bta/include/bta_gatt_api.h
@@ -364,6 +364,9 @@ typedef union
364364 BD_ADDR remote_bda; /* service change event */
365365 } tBTA_GATTC;
366366
367+/* GATTC enable callback function */
368+typedef void (tBTA_GATTC_ENB_CBACK)(tBTA_GATT_STATUS status);
369+
367370 /* Client callback function */
368371 typedef void (tBTA_GATTC_CBACK)(tBTA_GATTC_EVT event, tBTA_GATTC *p_data);
369372
@@ -547,9 +550,12 @@ typedef union
547550
548551 }tBTA_GATTS;
549552
553+/* GATTS enable callback function */
554+typedef void (tBTA_GATTS_ENB_CBACK)(tBTA_GATT_STATUS status);
550555
551556 /* Server callback function */
552557 typedef void (tBTA_GATTS_CBACK)(tBTA_GATTS_EVT event, tBTA_GATTS *p_data);
558+
553559 /*****************************************************************************
554560 ** External Function Declarations
555561 *****************************************************************************/
@@ -565,16 +571,16 @@ extern "C"
565571
566572 /*******************************************************************************
567573 **
568-** Function BTA_GATTC_Init
574+** Function BTA_GATTC_Disable
569575 **
570-** Description This function is called to initalize GATTC module
576+** Description This function is called to disable the GATTC module
571577 **
572-** Parameters None
578+** Parameters None.
573579 **
574580 ** Returns None
575581 **
576582 *******************************************************************************/
577-BTA_API extern void BTA_GATTC_Init();
583+BTA_API extern void BTA_GATTC_Disable(void);
578584
579585 /*******************************************************************************
580586 **
@@ -1017,6 +1023,19 @@ BTA_API extern void BTA_GATTC_Refresh(BD_ADDR remote_bda);
10171023
10181024 /*******************************************************************************
10191025 **
1026+** Function BTA_GATTS_Disable
1027+**
1028+** Description This function is called to disable GATTS module
1029+**
1030+** Parameters None.
1031+**
1032+** Returns None
1033+**
1034+*******************************************************************************/
1035+ BTA_API extern void BTA_GATTS_Disable(void);
1036+
1037+/*******************************************************************************
1038+**
10201039 ** Function BTA_GATTS_AppRegister
10211040 **
10221041 ** Description This function is called to register application callbacks
--- a/btif/src/btif_core.c
+++ b/btif/src/btif_core.c
@@ -1417,7 +1417,7 @@ bt_status_t btif_enable_service(tBTA_SERVICE_ID service_id)
14171417
14181418 btif_enabled_services |= (1 << service_id);
14191419
1420- BTIF_TRACE_ERROR2("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
1420+ BTIF_TRACE_DEBUG2("%s: current services:0x%x", __FUNCTION__, btif_enabled_services);
14211421
14221422 if (btif_is_enabled())
14231423 {
@@ -1450,7 +1450,7 @@ bt_status_t btif_disable_service(tBTA_SERVICE_ID service_id)
14501450
14511451 btif_enabled_services &= (tBTA_SERVICE_MASK)(~(1<<service_id));
14521452
1453- BTIF_TRACE_ERROR2("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
1453+ BTIF_TRACE_DEBUG2("%s: Current Services:0x%x", __FUNCTION__, btif_enabled_services);
14541454
14551455 if (btif_is_enabled())
14561456 {
--- a/btif/src/btif_gatt.c
+++ b/btif/src/btif_gatt.c
@@ -58,16 +58,13 @@ extern btgatt_server_interface_t btgattServerInterface;
5858 **
5959 ** Description Initializes the GATT interface
6060 **
61-** Returns s bt_status_t
61+** Returns bt_status_t
6262 **
6363 *******************************************************************************/
6464 static bt_status_t btif_gatt_init( const btgatt_callbacks_t* callbacks )
6565 {
6666 bt_gatt_callbacks = callbacks;
6767
68- BTA_GATTC_Init();
69- BTA_GATTS_Init();
70-
7168 return BT_STATUS_SUCCESS;
7269 }
7370
@@ -84,6 +81,9 @@ static void btif_gatt_cleanup( void )
8481 {
8582 if (bt_gatt_callbacks)
8683 bt_gatt_callbacks = NULL;
84+
85+ BTA_GATTC_Disable();
86+ BTA_GATTS_Disable();
8787 }
8888
8989 static const btgatt_interface_t btgattInterface = {
--- a/stack/gatt/gatt_api.c
+++ b/stack/gatt/gatt_api.c
@@ -1293,6 +1293,9 @@ void GATT_Deregister (tGATT_IF gatt_if)
12931293 }
12941294
12951295 gatt_deregister_bgdev_list(gatt_if);
1296+ /* update the listen mode */
1297+ GATT_Listen(gatt_if, FALSE, NULL);
1298+
12961299 memset (p_reg, 0, sizeof(tGATT_REG));
12971300 }
12981301
@@ -1564,7 +1567,7 @@ BOOLEAN GATT_GetConnIdIfConnected(tGATT_IF gatt_if, BD_ADDR bd_addr, UINT16 *p_c
15641567 ** Parameters gatt_if: applicaiton interface
15651568 ** p_bd_addr: listen for specific address connection, or NULL for
15661569 ** listen to all device connection.
1567-** start: is a direct conenection or a background auto connection
1570+** start: start or stop listening.
15681571 **
15691572 ** Returns TRUE if advertisement is started; FALSE if adv start failure.
15701573 **
--- a/stack/gatt/gatt_utils.c
+++ b/stack/gatt/gatt_utils.c
@@ -2322,6 +2322,8 @@ BOOLEAN gatt_add_bg_dev_list(tGATT_REG *p_reg, BD_ADDR bd_addr, BOOLEAN is_init
23222322 p_dev->gatt_if[i] = gatt_if;
23232323 if (i == 0)
23242324 ret = BTM_BleUpdateBgConnDev(TRUE, bd_addr);
2325+ else
2326+ ret = TRUE;
23252327 break;
23262328 }
23272329 }
@@ -2342,6 +2344,8 @@ BOOLEAN gatt_add_bg_dev_list(tGATT_REG *p_reg, BD_ADDR bd_addr, BOOLEAN is_init
23422344
23432345 if (i == 0)
23442346 ret = BTM_BleUpdateAdvWhitelist(TRUE, bd_addr);
2347+ else
2348+ ret = TRUE;
23452349 break;
23462350 }
23472351 }
@@ -2515,26 +2519,38 @@ void gatt_deregister_bgdev_list(tGATT_IF gatt_if)
25152519 {
25162520 tGATT_BG_CONN_DEV *p_dev_list = &gatt_cb.bgconn_dev[0];
25172521 UINT8 i , j, k;
2522+ tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
25182523
2524+ /* update the BG conn device list */
25192525 for (i = 0 ; i <GATT_MAX_BG_CONN_DEV; i ++, p_dev_list ++ )
25202526 {
25212527 if (p_dev_list->in_use)
25222528 {
25232529 for (j = 0; j < GATT_MAX_APPS; j ++)
25242530 {
2525- if (p_dev_list->gatt_if[j] == 0)
2531+ if (p_dev_list->gatt_if[j] == 0 && p_dev_list->listen_gif[j] == 0)
25262532 break;
2527- else if (p_dev_list->gatt_if[j] == gatt_if)
2533+
2534+ if (p_dev_list->gatt_if[j] == gatt_if)
25282535 {
25292536 for (k = j + 1; k < GATT_MAX_APPS; k ++)
25302537 p_dev_list->gatt_if[k - 1] = p_dev_list->gatt_if[k];
25312538
25322539 if (p_dev_list->gatt_if[0] == 0)
2533- {
25342540 BTM_BleUpdateBgConnDev(FALSE, p_dev_list->remote_bda);
2535- memset(p_dev_list, 0, sizeof(tGATT_BG_CONN_DEV));
2536- break;
2537- }
2541+ }
2542+
2543+ if (p_dev_list->listen_gif[j] == gatt_if)
2544+ {
2545+ p_dev_list->listen_gif[j] = 0;
2546+ p_reg->listening --;
2547+
2548+ /* move all element behind one forward */
2549+ for (k = j + 1; k < GATT_MAX_APPS; k ++)
2550+ p_dev_list->listen_gif[k - 1] = p_dev_list->listen_gif[k];
2551+
2552+ if (p_dev_list->listen_gif[0] == 0)
2553+ BTM_BleUpdateAdvWhitelist(FALSE, p_dev_list->remote_bda);
25382554 }
25392555 }
25402556 }