blob: fb5fcf84b2002cb56f5d36f786212f5504a9e377 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 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 file contains the GATT Server action functions for the state
22 * machine.
23 *
24 ******************************************************************************/
25
The Android Open Source Project5738f832012-12-12 16:00:35 -080026#include "bt_target.h"
27
The Android Open Source Project5738f832012-12-12 16:00:35 -080028#include <string.h>
Myles Watsoncd1fd072016-11-09 13:17:43 -080029#include "bt_common.h"
30#include "bta_gatts_co.h"
31#include "bta_gatts_int.h"
32#include "bta_sys.h"
33#include "btif/include/btif_debug_conn.h"
34#include "btm_ble_api.h"
35#include "osi/include/osi.h"
36#include "utl.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080037
Myles Watsoncd1fd072016-11-09 13:17:43 -080038static void bta_gatts_nv_save_cback(bool is_saved,
39 tGATTS_HNDL_RANGE* p_hndl_range);
40static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
41 tGATTS_SRV_CHG_REQ* p_req,
42 tGATTS_SRV_CHG_RSP* p_rsp);
The Android Open Source Project5738f832012-12-12 16:00:35 -080043
Myles Watsoncd1fd072016-11-09 13:17:43 -080044static void bta_gatts_conn_cback(tGATT_IF gatt_if, BD_ADDR bda,
45 uint16_t conn_id, bool connected,
46 tGATT_DISCONN_REASON reason,
47 tGATT_TRANSPORT transport);
48static void bta_gatts_send_request_cback(uint16_t conn_id, uint32_t trans_id,
49 tGATTS_REQ_TYPE req_type,
50 tGATTS_DATA* p_data);
51static void bta_gatts_cong_cback(uint16_t conn_id, bool congested);
Andre Eisenbach17b04bd2014-03-28 14:54:22 -070052
Myles Watsoncd1fd072016-11-09 13:17:43 -080053static tGATT_CBACK bta_gatts_cback = {
54 bta_gatts_conn_cback, NULL, NULL, NULL, bta_gatts_send_request_cback, NULL,
55 bta_gatts_cong_cback};
The Android Open Source Project5738f832012-12-12 16:00:35 -080056
Myles Watsoncd1fd072016-11-09 13:17:43 -080057tGATT_APPL_INFO bta_gatts_nv_cback = {bta_gatts_nv_save_cback,
58 bta_gatts_nv_srv_chg_cback};
The Android Open Source Project5738f832012-12-12 16:00:35 -080059
60/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080061 *
62 * Function bta_gatts_nv_save_cback
63 *
64 * Description NV save callback function.
65 *
Myles Watsoncd1fd072016-11-09 13:17:43 -080066 * Parameter is_add: true is to add a handle range; otherwise is to
Myles Watson1baaae32016-11-09 14:25:23 -080067 * delete.
Myles Watson8af480e2016-11-09 10:40:23 -080068 * Returns none.
69 *
70 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -080071static void bta_gatts_nv_save_cback(bool is_add,
72 tGATTS_HNDL_RANGE* p_hndl_range) {
73 bta_gatts_co_update_handle_range(is_add,
74 (tBTA_GATTS_HNDL_RANGE*)p_hndl_range);
The Android Open Source Project5738f832012-12-12 16:00:35 -080075}
76
The Android Open Source Project5738f832012-12-12 16:00:35 -080077/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080078 *
79 * Function bta_gatts_nv_srv_chg_cback
80 *
81 * Description NV save callback function.
82 *
Myles Watsoncd1fd072016-11-09 13:17:43 -080083 * Parameter is_add: true is to add a handle range; otherwise is to
Myles Watson1baaae32016-11-09 14:25:23 -080084 * delete.
Myles Watson8af480e2016-11-09 10:40:23 -080085 * Returns none.
86 *
87 ******************************************************************************/
Marie Janssene9e58ce2016-06-17 14:12:17 -070088static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
Myles Watsoncd1fd072016-11-09 13:17:43 -080089 tGATTS_SRV_CHG_REQ* p_req,
90 tGATTS_SRV_CHG_RSP* p_rsp) {
91 return bta_gatts_co_srv_chg((tBTA_GATTS_SRV_CHG_CMD)cmd,
92 (tBTA_GATTS_SRV_CHG_REQ*)p_req,
93 (tBTA_GATTS_SRV_CHG_RSP*)p_rsp);
The Android Open Source Project5738f832012-12-12 16:00:35 -080094}
95
The Android Open Source Project5738f832012-12-12 16:00:35 -080096/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -080097 *
98 * Function bta_gatts_enable
99 *
100 * Description enable BTA GATTS module.
101 *
102 * Returns none.
103 *
104 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800105void bta_gatts_enable(tBTA_GATTS_CB* p_cb) {
106 uint8_t index = 0;
107 tBTA_GATTS_HNDL_RANGE handle_range;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800108
Myles Watsoncd1fd072016-11-09 13:17:43 -0800109 if (p_cb->enabled) {
110 APPL_TRACE_DEBUG("GATTS already enabled.");
111 } else {
112 memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
113
114 p_cb->enabled = true;
115
116 while (bta_gatts_co_load_handle_range(index, &handle_range)) {
117 GATTS_AddHandleRange((tGATTS_HNDL_RANGE*)&handle_range);
118 memset(&handle_range, 0, sizeof(tGATTS_HNDL_RANGE));
119 index++;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800120 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700121
Myles Watsoncd1fd072016-11-09 13:17:43 -0800122 APPL_TRACE_DEBUG("bta_gatts_enable: num of handle range added=%d", index);
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700123
Myles Watsoncd1fd072016-11-09 13:17:43 -0800124 if (!GATTS_NVRegister(&bta_gatts_nv_cback)) {
125 APPL_TRACE_ERROR("BTA GATTS NV register failed.");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800126 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800127 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800128}
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700129
130/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800131 *
132 * Function bta_gatts_api_disable
133 *
134 * Description disable BTA GATTS module.
135 *
136 * Returns none.
137 *
138 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800139void bta_gatts_api_disable(tBTA_GATTS_CB* p_cb) {
140 uint8_t i;
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700141
Myles Watsoncd1fd072016-11-09 13:17:43 -0800142 if (p_cb->enabled) {
143 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
144 if (p_cb->rcb[i].in_use) {
145 GATT_Deregister(p_cb->rcb[i].gatt_if);
146 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700147 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800148 memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
149 } else {
150 APPL_TRACE_ERROR("GATTS not enabled");
151 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700152}
153
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800155 *
156 * Function bta_gatts_register
157 *
158 * Description register an application.
159 *
160 * Returns none.
161 *
162 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800163void bta_gatts_register(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
164 tBTA_GATTS cb_data;
165 tBTA_GATT_STATUS status = BTA_GATT_OK;
166 uint8_t i, first_unuse = 0xff;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800167
Myles Watsoncd1fd072016-11-09 13:17:43 -0800168 if (p_cb->enabled == false) {
169 bta_gatts_enable(p_cb);
170 }
171
172 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
173 if (p_cb->rcb[i].in_use) {
174 if (bta_gatts_uuid_compare(p_cb->rcb[i].app_uuid,
175 p_msg->api_reg.app_uuid)) {
176 APPL_TRACE_ERROR("application already registered.");
177 status = BTA_GATT_DUP_REG;
178 break;
179 }
180 }
181 }
182
183 if (status == BTA_GATT_OK) {
184 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
185 if (first_unuse == 0xff && !p_cb->rcb[i].in_use) {
186 first_unuse = i;
187 break;
188 }
Andre Eisenbache1202ca2013-05-15 04:55:08 -0700189 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800190
Myles Watsoncd1fd072016-11-09 13:17:43 -0800191 cb_data.reg_oper.server_if = BTA_GATTS_INVALID_IF;
192 memcpy(&cb_data.reg_oper.uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID));
193 if (first_unuse != 0xff) {
194 APPL_TRACE_ERROR("register application first_unuse rcb_idx = %d",
195 first_unuse);
196
197 p_cb->rcb[first_unuse].in_use = true;
198 p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback;
199 memcpy(&p_cb->rcb[first_unuse].app_uuid, &p_msg->api_reg.app_uuid,
200 sizeof(tBT_UUID));
201 cb_data.reg_oper.server_if = p_cb->rcb[first_unuse].gatt_if =
202 GATT_Register(&p_msg->api_reg.app_uuid, &bta_gatts_cback);
203 if (!p_cb->rcb[first_unuse].gatt_if) {
204 status = BTA_GATT_NO_RESOURCES;
205 } else {
206 tBTA_GATTS_INT_START_IF* p_buf = (tBTA_GATTS_INT_START_IF*)osi_malloc(
207 sizeof(tBTA_GATTS_INT_START_IF));
208 p_buf->hdr.event = BTA_GATTS_INT_START_IF_EVT;
209 p_buf->server_if = p_cb->rcb[first_unuse].gatt_if;
210
211 bta_sys_sendmsg(p_buf);
212 }
213 } else {
214 status = BTA_GATT_NO_RESOURCES;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800215 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800216 }
217 cb_data.reg_oper.status = status;
218 if (p_msg->api_reg.p_cback)
219 (*p_msg->api_reg.p_cback)(BTA_GATTS_REG_EVT, &cb_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800220}
221
The Android Open Source Project5738f832012-12-12 16:00:35 -0800222/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800223 *
224 * Function bta_gatts_start_if
225 *
226 * Description start an application interface.
227 *
228 * Returns none.
229 *
230 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800231void bta_gatts_start_if(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
232 tBTA_GATTS_DATA* p_msg) {
233 if (bta_gatts_find_app_rcb_by_app_if(p_msg->int_start_if.server_if)) {
234 GATT_StartIf(p_msg->int_start_if.server_if);
235 } else {
236 APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",
237 p_msg->int_start_if.server_if);
238 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800239}
240/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800241 *
242 * Function bta_gatts_deregister
243 *
244 * Description deregister an application.
245 *
246 * Returns none.
247 *
248 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800249void bta_gatts_deregister(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
250 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
251 tBTA_GATTS_CBACK* p_cback = NULL;
252 uint8_t i;
253 tBTA_GATTS cb_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800254
Myles Watsoncd1fd072016-11-09 13:17:43 -0800255 cb_data.reg_oper.server_if = p_msg->api_dereg.server_if;
256 cb_data.reg_oper.status = status;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800257
Myles Watsoncd1fd072016-11-09 13:17:43 -0800258 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
259 if (p_cb->rcb[i].in_use &&
260 p_cb->rcb[i].gatt_if == p_msg->api_dereg.server_if) {
261 p_cback = p_cb->rcb[i].p_cback;
262 status = BTA_GATT_OK;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800263
Myles Watsoncd1fd072016-11-09 13:17:43 -0800264 /* deregister the app */
265 GATT_Deregister(p_cb->rcb[i].gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800266
Myles Watsoncd1fd072016-11-09 13:17:43 -0800267 /* reset cb */
268 memset(&p_cb->rcb[i], 0, sizeof(tBTA_GATTS_RCB));
269 cb_data.reg_oper.status = status;
270 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800272 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800273
Myles Watsoncd1fd072016-11-09 13:17:43 -0800274 if (p_cback) {
275 (*p_cback)(BTA_GATTS_DEREG_EVT, &cb_data);
276 } else {
277 APPL_TRACE_ERROR("application not registered.");
278 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800279}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800280
The Android Open Source Project5738f832012-12-12 16:00:35 -0800281/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800282 *
283 * Function bta_gatts_delete_service
284 *
285 * Description action function to delete a service.
286 *
287 * Returns none.
288 *
289 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800290void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB* p_srvc_cb,
291 tBTA_GATTS_DATA* p_msg) {
292 tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
293 tBTA_GATTS cb_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800294
Myles Watsoncd1fd072016-11-09 13:17:43 -0800295 cb_data.srvc_oper.server_if = p_rcb->gatt_if;
296 // cb_data.srvc_oper.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800297
Myles Watsoncd1fd072016-11-09 13:17:43 -0800298 if (GATTS_DeleteService(p_rcb->gatt_if, &p_srvc_cb->service_uuid,
299 p_srvc_cb->service_id)) {
300 cb_data.srvc_oper.status = BTA_GATT_OK;
301 memset(p_srvc_cb, 0, sizeof(tBTA_GATTS_SRVC_CB));
302 } else {
303 cb_data.srvc_oper.status = BTA_GATT_ERROR;
304 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800305
Myles Watsoncd1fd072016-11-09 13:17:43 -0800306 if (p_rcb->p_cback) (*p_rcb->p_cback)(BTA_GATTS_DELELTE_EVT, &cb_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800307}
The Android Open Source Project5738f832012-12-12 16:00:35 -0800308
The Android Open Source Project5738f832012-12-12 16:00:35 -0800309/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800310 *
311 * Function bta_gatts_stop_service
312 *
313 * Description action function to stop a service.
314 *
315 * Returns none.
316 *
317 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800318void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB* p_srvc_cb,
319 UNUSED_ATTR tBTA_GATTS_DATA* p_msg) {
320 tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
321 tBTA_GATTS cb_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800322
Myles Watsoncd1fd072016-11-09 13:17:43 -0800323 GATTS_StopService(p_srvc_cb->service_id);
324 cb_data.srvc_oper.server_if = p_rcb->gatt_if;
325 cb_data.srvc_oper.service_id = p_srvc_cb->service_id;
326 cb_data.srvc_oper.status = BTA_GATT_OK;
327 APPL_TRACE_ERROR("bta_gatts_stop_service service_id= %d",
328 p_srvc_cb->service_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800329
Myles Watsoncd1fd072016-11-09 13:17:43 -0800330 if (p_rcb->p_cback) (*p_rcb->p_cback)(BTA_GATTS_STOP_EVT, &cb_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800331}
332/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800333 *
334 * Function bta_gatts_send_rsp
335 *
336 * Description GATTS send response.
337 *
338 * Returns none.
339 *
340 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800341void bta_gatts_send_rsp(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
342 tBTA_GATTS_DATA* p_msg) {
343 if (GATTS_SendRsp(p_msg->api_rsp.hdr.layer_specific, p_msg->api_rsp.trans_id,
344 p_msg->api_rsp.status,
345 (tGATTS_RSP*)p_msg->api_rsp.p_rsp) != GATT_SUCCESS) {
346 APPL_TRACE_ERROR("Sending response failed");
347 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800348}
349/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800350 *
351 * Function bta_gatts_indicate_handle
352 *
353 * Description GATTS send handle value indication or notification.
354 *
355 * Returns none.
356 *
357 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800358void bta_gatts_indicate_handle(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
359 tBTA_GATTS_SRVC_CB* p_srvc_cb;
360 tBTA_GATTS_RCB* p_rcb = NULL;
361 tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
362 tGATT_IF gatt_if;
363 BD_ADDR remote_bda;
364 tBTA_TRANSPORT transport;
365 tBTA_GATTS cb_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800366
Myles Watsoncd1fd072016-11-09 13:17:43 -0800367 p_srvc_cb =
368 bta_gatts_find_srvc_cb_by_attr_id(p_cb, p_msg->api_indicate.attr_id);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369
Myles Watsoncd1fd072016-11-09 13:17:43 -0800370 if (p_srvc_cb) {
371 if (GATT_GetConnectionInfor(p_msg->api_indicate.hdr.layer_specific,
372 &gatt_if, remote_bda, &transport)) {
373 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700374
Myles Watsoncd1fd072016-11-09 13:17:43 -0800375 if (p_msg->api_indicate.need_confirm)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800376
Myles Watsoncd1fd072016-11-09 13:17:43 -0800377 status = GATTS_HandleValueIndication(
378 p_msg->api_indicate.hdr.layer_specific, p_msg->api_indicate.attr_id,
379 p_msg->api_indicate.len, p_msg->api_indicate.value);
380 else
381 status = GATTS_HandleValueNotification(
382 p_msg->api_indicate.hdr.layer_specific, p_msg->api_indicate.attr_id,
383 p_msg->api_indicate.len, p_msg->api_indicate.value);
Andre Eisenbach781b5ad2013-05-28 15:11:43 +0900384
Myles Watsoncd1fd072016-11-09 13:17:43 -0800385 /* if over BR_EDR, inform PM for mode change */
386 if (transport == BTA_TRANSPORT_BR_EDR) {
387 bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
388 bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
389 }
390 } else {
391 APPL_TRACE_ERROR("Unknown connection ID: %d fail sending notification",
392 p_msg->api_indicate.hdr.layer_specific);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800393 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800394
395 if ((status != GATT_SUCCESS || !p_msg->api_indicate.need_confirm) &&
396 p_rcb && p_cb->rcb[p_srvc_cb->rcb_idx].p_cback) {
397 cb_data.req_data.status = status;
398 cb_data.req_data.conn_id = p_msg->api_indicate.hdr.layer_specific;
399
400 (*p_rcb->p_cback)(BTA_GATTS_CONF_EVT, &cb_data);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800401 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800402 } else {
403 APPL_TRACE_ERROR("Not an registered servce attribute ID: 0x%04x",
404 p_msg->api_indicate.attr_id);
405 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800406}
407
The Android Open Source Project5738f832012-12-12 16:00:35 -0800408/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800409 *
410 * Function bta_gatts_open
411 *
412 * Description
413 *
414 * Returns none.
415 *
416 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800417void bta_gatts_open(UNUSED_ATTR tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
418 tBTA_GATTS_RCB* p_rcb = NULL;
419 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
420 uint16_t conn_id;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800421
Marie Janssenf33b6f42016-11-22 15:01:42 -0800422 p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if);
423 if (p_rcb != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800424 /* should always get the connection ID */
425 if (GATT_Connect(p_rcb->gatt_if, p_msg->api_open.remote_bda,
426 p_msg->api_open.is_direct, p_msg->api_open.transport,
427 false)) {
428 status = BTA_GATT_OK;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700429
Myles Watsoncd1fd072016-11-09 13:17:43 -0800430 if (GATT_GetConnIdIfConnected(p_rcb->gatt_if, p_msg->api_open.remote_bda,
431 &conn_id, p_msg->api_open.transport)) {
432 status = BTA_GATT_ALREADY_OPEN;
433 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800434 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800435 } else {
436 APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_open.server_if);
437 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800438
Myles Watsoncd1fd072016-11-09 13:17:43 -0800439 if (p_rcb && p_rcb->p_cback)
440 (*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, (tBTA_GATTS*)&status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800441}
442/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800443 *
444 * Function bta_gatts_cancel_open
445 *
446 * Description
447 *
448 * Returns none.
449 *
450 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800451void bta_gatts_cancel_open(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
452 tBTA_GATTS_DATA* p_msg) {
453 tBTA_GATTS_RCB* p_rcb;
454 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800455
Marie Janssenf33b6f42016-11-22 15:01:42 -0800456 p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if);
457 if (p_rcb != NULL) {
Myles Watsoncd1fd072016-11-09 13:17:43 -0800458 if (!GATT_CancelConnect(p_rcb->gatt_if, p_msg->api_cancel_open.remote_bda,
459 p_msg->api_cancel_open.is_direct)) {
460 APPL_TRACE_ERROR("bta_gatts_cancel_open failed for open request");
461 } else {
462 status = BTA_GATT_OK;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800463 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800464 } else {
465 APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_cancel_open.server_if);
466 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800467
Myles Watsoncd1fd072016-11-09 13:17:43 -0800468 if (p_rcb && p_rcb->p_cback)
469 (*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, (tBTA_GATTS*)&status);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800470}
471/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800472 *
473 * Function bta_gatts_close
474 *
475 * Description
476 *
477 * Returns none.
478 *
479 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800480void bta_gatts_close(UNUSED_ATTR tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
481 tBTA_GATTS_RCB* p_rcb;
482 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
483 tGATT_IF gatt_if;
484 BD_ADDR remote_bda;
485 tBTA_GATT_TRANSPORT transport;
Ganesh Ganapathi Batta7fa4fba2014-04-16 16:50:09 -0700486
Myles Watsoncd1fd072016-11-09 13:17:43 -0800487 if (GATT_GetConnectionInfor(p_msg->hdr.layer_specific, &gatt_if, remote_bda,
488 &transport)) {
489 if (GATT_Disconnect(p_msg->hdr.layer_specific) != GATT_SUCCESS) {
490 APPL_TRACE_ERROR("bta_gatts_close fail conn_id=%d",
491 p_msg->hdr.layer_specific);
492 } else {
493 status = BTA_GATT_OK;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800494 }
495
Myles Watsoncd1fd072016-11-09 13:17:43 -0800496 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
497
498 if (p_rcb && p_rcb->p_cback) {
499 if (transport == BTA_TRANSPORT_BR_EDR)
500 bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
501
502 (*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, (tBTA_GATTS*)&status);
503 }
504 } else {
505 APPL_TRACE_ERROR("Unknown connection ID: %d", p_msg->hdr.layer_specific);
506 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800507}
508
The Android Open Source Project5738f832012-12-12 16:00:35 -0800509/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800510 *
511 * Function bta_gatts_request_cback
512 *
513 * Description GATTS attribute request callback.
514 *
515 * Returns none.
516 *
517 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800518static void bta_gatts_send_request_cback(uint16_t conn_id, uint32_t trans_id,
519 tGATTS_REQ_TYPE req_type,
520 tGATTS_DATA* p_data) {
521 tBTA_GATTS cb_data;
522 tBTA_GATTS_RCB* p_rcb;
523 tGATT_IF gatt_if;
524 tBTA_GATT_TRANSPORT transport;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800525
Myles Watsoncd1fd072016-11-09 13:17:43 -0800526 memset(&cb_data, 0, sizeof(tBTA_GATTS));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800527
Myles Watsoncd1fd072016-11-09 13:17:43 -0800528 if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda,
529 &transport)) {
530 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800531
Myles Watsoncd1fd072016-11-09 13:17:43 -0800532 APPL_TRACE_DEBUG("%s: conn_id=%d trans_id=%d req_type=%d", __func__,
533 conn_id, trans_id, req_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800534
Myles Watsoncd1fd072016-11-09 13:17:43 -0800535 if (p_rcb && p_rcb->p_cback) {
536 /* if over BR_EDR, inform PM for mode change */
537 if (transport == BTA_TRANSPORT_BR_EDR) {
538 bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
539 bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
540 }
Andre Eisenbach781b5ad2013-05-28 15:11:43 +0900541
Myles Watsoncd1fd072016-11-09 13:17:43 -0800542 cb_data.req_data.conn_id = conn_id;
543 cb_data.req_data.trans_id = trans_id;
544 cb_data.req_data.p_data = (tBTA_GATTS_REQ_DATA*)p_data;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800545
Myles Watsoncd1fd072016-11-09 13:17:43 -0800546 (*p_rcb->p_cback)(req_type, &cb_data);
547 } else {
548 APPL_TRACE_ERROR("connection request on gatt_if[%d] is not interested",
549 gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800550 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800551 } else {
552 APPL_TRACE_ERROR("request received on unknown connectino ID: %d", conn_id);
553 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800554}
555
556/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800557 *
558 * Function bta_gatts_conn_cback
559 *
560 * Description connection callback.
561 *
562 * Returns none.
563 *
564 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800565static void bta_gatts_conn_cback(tGATT_IF gatt_if, BD_ADDR bda,
566 uint16_t conn_id, bool connected,
567 tGATT_DISCONN_REASON reason,
568 tGATT_TRANSPORT transport) {
569 tBTA_GATTS cb_data;
570 uint8_t evt = connected ? BTA_GATTS_CONNECT_EVT : BTA_GATTS_DISCONNECT_EVT;
571 tBTA_GATTS_RCB* p_reg;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800572
Myles Watsoncd1fd072016-11-09 13:17:43 -0800573 APPL_TRACE_DEBUG(
574 "bta_gatts_conn_cback gatt_if=%d conn_id=%d connected=%d reason = 0x%04d",
575 gatt_if, conn_id, connected, reason);
576 APPL_TRACE_DEBUG("bta_gatts_conn_cback bda :%02x-%02x-%02x-%02x-%02x-%02x ",
577 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800578
Myles Watsoncd1fd072016-11-09 13:17:43 -0800579 bt_bdaddr_t bdaddr;
580 bdcpy(bdaddr.address, bda);
581 if (connected)
582 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
583 else
584 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
Andre Eisenbach89f5e412014-12-05 09:40:20 -0800585
Myles Watsoncd1fd072016-11-09 13:17:43 -0800586 p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800587
Myles Watsoncd1fd072016-11-09 13:17:43 -0800588 if (p_reg && p_reg->p_cback) {
589 /* there is no RM for GATT */
590 if (transport == BTA_TRANSPORT_BR_EDR) {
591 if (connected)
592 bta_sys_conn_open(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
593 else
594 bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800595 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800596
597 cb_data.conn.conn_id = conn_id;
598 cb_data.conn.server_if = gatt_if;
599 cb_data.conn.reason = reason;
600 cb_data.conn.transport = transport;
601 memcpy(cb_data.conn.remote_bda, bda, BD_ADDR_LEN);
602 (*p_reg->p_cback)(evt, &cb_data);
603 } else {
604 APPL_TRACE_ERROR("bta_gatts_conn_cback server_if=%d not found", gatt_if);
605 }
The Android Open Source Project5738f832012-12-12 16:00:35 -0800606}
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700607
608/*******************************************************************************
Myles Watson8af480e2016-11-09 10:40:23 -0800609 *
610 * Function bta_gatts_cong_cback
611 *
612 * Description congestion callback.
613 *
614 * Returns none.
615 *
616 ******************************************************************************/
Myles Watsoncd1fd072016-11-09 13:17:43 -0800617static void bta_gatts_cong_cback(uint16_t conn_id, bool congested) {
618 tBTA_GATTS_RCB* p_rcb;
619 tGATT_IF gatt_if;
620 tBTA_GATT_TRANSPORT transport;
621 tBTA_GATTS cb_data;
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700622
Myles Watsoncd1fd072016-11-09 13:17:43 -0800623 if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda,
624 &transport)) {
625 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700626
Myles Watsoncd1fd072016-11-09 13:17:43 -0800627 if (p_rcb && p_rcb->p_cback) {
628 cb_data.congest.conn_id = conn_id;
629 cb_data.congest.congested = congested;
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700630
Myles Watsoncd1fd072016-11-09 13:17:43 -0800631 (*p_rcb->p_cback)(BTA_GATTS_CONGEST_EVT, &cb_data);
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700632 }
Myles Watsoncd1fd072016-11-09 13:17:43 -0800633 }
Andre Eisenbach17b04bd2014-03-28 14:54:22 -0700634}