1 /******************************************************************************
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
26
27 #include "common/bt_target.h"
28
29 #if defined(GATTS_INCLUDED) && (GATTS_INCLUDED == TRUE)
30
31 #include "bta/utl.h"
32 #include "bta/bta_sys.h"
33 #include "bta_gatts_int.h"
34 #include "bta/bta_gatts_co.h"
35 #include "stack/btm_ble_api.h"
36 #include <string.h>
37 #include "osi/allocator.h"
38 #include "l2c_int.h"
39
40 static void bta_gatts_nv_save_cback(BOOLEAN is_saved, tGATTS_HNDL_RANGE *p_hndl_range);
41 static BOOLEAN bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd, tGATTS_SRV_CHG_REQ *p_req,
42 tGATTS_SRV_CHG_RSP *p_rsp);
43
44 static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
45 BOOLEAN connected, tGATT_DISCONN_REASON reason,
46 tGATT_TRANSPORT transport);
47 static void bta_gatts_send_request_cback (UINT16 conn_id,
48 UINT32 trans_id,
49 tGATTS_REQ_TYPE req_type, tGATTS_DATA *p_data);
50 static void bta_gatts_cong_cback (UINT16 conn_id, BOOLEAN congested);
51 extern void btc_congest_callback(tBTA_GATTS *param);
52
53 static const tGATT_CBACK bta_gatts_cback = {
54 bta_gatts_conn_cback,
55 NULL,
56 NULL,
57 NULL,
58 bta_gatts_send_request_cback,
59 NULL,
60 bta_gatts_cong_cback
61 };
62
63 const tGATT_APPL_INFO bta_gatts_nv_cback = {
64 bta_gatts_nv_save_cback,
65 bta_gatts_nv_srv_chg_cback
66 };
67
68 /*******************************************************************************
69 **
70 ** Function bta_gatts_nv_save_cback
71 **
72 ** Description NV save callback function.
73 **
74 ** Parameter is_add: true is to add a handle range; otherwise is to delete.
75 ** Returns none.
76 **
77 *******************************************************************************/
bta_gatts_nv_save_cback(BOOLEAN is_add,tGATTS_HNDL_RANGE * p_hndl_range)78 static void bta_gatts_nv_save_cback(BOOLEAN is_add, tGATTS_HNDL_RANGE *p_hndl_range)
79 {
80 bta_gatts_co_update_handle_range(is_add, (tBTA_GATTS_HNDL_RANGE *)p_hndl_range);
81 }
82
83
84 /*******************************************************************************
85 **
86 ** Function bta_gatts_nv_srv_chg_cback
87 **
88 ** Description NV save callback function.
89 **
90 ** Parameter is_add: true is to add a handle range; otherwise is to delete.
91 ** Returns none.
92 **
93 *******************************************************************************/
bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,tGATTS_SRV_CHG_REQ * p_req,tGATTS_SRV_CHG_RSP * p_rsp)94 static BOOLEAN bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
95 tGATTS_SRV_CHG_REQ *p_req, tGATTS_SRV_CHG_RSP *p_rsp)
96 {
97 return bta_gatts_co_srv_chg((tBTA_GATTS_SRV_CHG_CMD) cmd,
98 (tBTA_GATTS_SRV_CHG_REQ *) p_req,
99 (tBTA_GATTS_SRV_CHG_RSP *) p_rsp);
100 }
101
102
103 /*******************************************************************************
104 **
105 ** Function bta_gatts_enable
106 **
107 ** Description enable BTA GATTS module.
108 **
109 ** Returns none.
110 **
111 *******************************************************************************/
bta_gatts_enable(tBTA_GATTS_CB * p_cb)112 void bta_gatts_enable(tBTA_GATTS_CB *p_cb)
113 {
114 UINT8 index = 0;
115 tBTA_GATTS_HNDL_RANGE handle_range;
116
117 if (p_cb->enabled) {
118 APPL_TRACE_DEBUG("GATTS already enabled.");
119 } else {
120 memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
121
122 p_cb->enabled = TRUE;
123
124 while ( bta_gatts_co_load_handle_range(index, &handle_range)) {
125 GATTS_AddHandleRange((tGATTS_HNDL_RANGE *)&handle_range);
126 memset(&handle_range, 0, sizeof(tGATTS_HNDL_RANGE));
127 index++;
128 }
129
130 APPL_TRACE_DEBUG("bta_gatts_enable: num of handle range added=%d", index);
131
132 if (!GATTS_NVRegister(&bta_gatts_nv_cback)) {
133 APPL_TRACE_ERROR("BTA GATTS NV register failed.");
134 }
135 }
136 }
137
138 /*******************************************************************************
139 **
140 ** Function bta_gatts_api_disable
141 **
142 ** Description disable BTA GATTS module.
143 **
144 ** Returns none.
145 **
146 *******************************************************************************/
bta_gatts_api_disable(tBTA_GATTS_CB * p_cb)147 void bta_gatts_api_disable(tBTA_GATTS_CB *p_cb)
148 {
149 UINT8 i;
150
151 if (p_cb->enabled) {
152 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++) {
153 if (p_cb->rcb[i].in_use) {
154 GATT_Deregister(p_cb->rcb[i].gatt_if);
155 }
156 }
157 memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
158 } else {
159 APPL_TRACE_ERROR("GATTS not enabled");
160 }
161 }
162
163 /*******************************************************************************
164 **
165 ** Function bta_gatts_register
166 **
167 ** Description register an application.
168 **
169 ** Returns none.
170 **
171 *******************************************************************************/
bta_gatts_register(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)172 void bta_gatts_register(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
173 {
174 tBTA_GATTS_INT_START_IF *p_buf;
175 tBTA_GATTS cb_data;
176 tBTA_GATT_STATUS status = BTA_GATT_OK;
177 UINT8 i, first_unuse = 0xff;
178
179 if (p_cb->enabled == FALSE) {
180 bta_gatts_enable(p_cb);
181 }
182
183 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++) {
184 if (p_cb->rcb[i].in_use) {
185 if (bta_gatts_uuid_compare(p_cb->rcb[i].app_uuid, p_msg->api_reg.app_uuid)) {
186 APPL_TRACE_ERROR("application already registered.\n");
187 status = BTA_GATT_DUP_REG;
188 break;
189 }
190 }
191 }
192
193 if (status == BTA_GATT_OK) {
194 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++) {
195 if (first_unuse == 0xff && !p_cb->rcb[i].in_use) {
196 first_unuse = i;
197 break;
198 }
199 }
200
201 cb_data.reg_oper.server_if = BTA_GATTS_INVALID_IF;
202 // btla-specific ++
203 memcpy(&cb_data.reg_oper.uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID));
204 // btla-specific --
205 if (first_unuse != 0xff) {
206 APPL_TRACE_VERBOSE("register application first_unuse rcb_idx = %d", first_unuse);
207
208 p_cb->rcb[first_unuse].in_use = TRUE;
209 p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback;
210 memcpy(&p_cb->rcb[first_unuse].app_uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID));
211 cb_data.reg_oper.server_if =
212 p_cb->rcb[first_unuse].gatt_if =
213 GATT_Register(&p_msg->api_reg.app_uuid, &bta_gatts_cback);
214 if ( !p_cb->rcb[first_unuse].gatt_if) {
215 status = BTA_GATT_NO_RESOURCES;
216 } else {
217 if ((p_buf =
218 (tBTA_GATTS_INT_START_IF *) osi_malloc(sizeof(tBTA_GATTS_INT_START_IF))) != NULL) {
219 p_buf->hdr.event = BTA_GATTS_INT_START_IF_EVT;
220 p_buf->server_if = p_cb->rcb[first_unuse].gatt_if;
221
222 bta_sys_sendmsg(p_buf);
223 } else {
224 status = BTA_GATT_NO_RESOURCES;
225 memset( &p_cb->rcb[first_unuse], 0 , sizeof(tBTA_GATTS_RCB));
226 }
227 }
228 } else {
229 status = BTA_GATT_NO_RESOURCES;
230 }
231
232 }
233 cb_data.reg_oper.status = status;
234 if (p_msg->api_reg.p_cback) {
235 (*p_msg->api_reg.p_cback)(BTA_GATTS_REG_EVT, &cb_data);
236 }
237 }
238
239
240 /*******************************************************************************
241 **
242 ** Function bta_gatts_start_if
243 **
244 ** Description start an application interface.
245 **
246 ** Returns none.
247 **
248 *******************************************************************************/
bta_gatts_start_if(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)249 void bta_gatts_start_if(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
250 {
251 UNUSED(p_cb);
252
253 if (bta_gatts_find_app_rcb_by_app_if(p_msg->int_start_if.server_if)) {
254 GATT_StartIf(p_msg->int_start_if.server_if);
255 } else {
256 APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",
257 p_msg->int_start_if.server_if );
258 }
259 }
260 /*******************************************************************************
261 **
262 ** Function bta_gatts_deregister
263 **
264 ** Description deregister an application.
265 **
266 ** Returns none.
267 **
268 *******************************************************************************/
bta_gatts_deregister(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)269 void bta_gatts_deregister(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
270 {
271 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
272 tBTA_GATTS_CBACK *p_cback = NULL;
273 UINT8 i;
274 tBTA_GATTS cb_data;
275
276 cb_data.reg_oper.server_if = p_msg->api_dereg.server_if;
277 cb_data.reg_oper.status = status;
278
279 for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i ++) {
280 if (p_cb->rcb[i].in_use && p_cb->rcb[i].gatt_if == p_msg->api_dereg.server_if) {
281 p_cback = p_cb->rcb[i].p_cback;
282 status = BTA_GATT_OK;
283
284 /* deregister the app */
285 GATT_Deregister(p_cb->rcb[i].gatt_if);
286
287 /* reset cb */
288 memset(&p_cb->rcb[i], 0, sizeof(tBTA_GATTS_RCB));
289 cb_data.reg_oper.status = status;
290 break;
291 }
292 }
293
294 if (p_cback) {
295 (*p_cback)(BTA_GATTS_DEREG_EVT, &cb_data);
296 } else {
297 APPL_TRACE_ERROR("application not registered.");
298 }
299 }
300 /*******************************************************************************
301 **
302 ** Function bta_gatts_create_srvc
303 **
304 ** Description action function to create a service.
305 **
306 ** Returns none.
307 **
308 *******************************************************************************/
bta_gatts_create_srvc(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)309 void bta_gatts_create_srvc(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
310 {
311 UINT8 rcb_idx;
312 tBTA_GATTS cb_data;
313 UINT8 srvc_idx;
314 UINT16 service_id = 0;
315
316 cb_data.create.status = BTA_GATT_ERROR;
317
318 rcb_idx = bta_gatts_find_app_rcb_idx_by_app_if(p_cb, p_msg->api_create_svc.server_if);
319
320 APPL_TRACE_DEBUG("create service rcb_idx = %d", rcb_idx);
321
322 if (rcb_idx != BTA_GATTS_INVALID_APP) {
323 if ((srvc_idx = bta_gatts_alloc_srvc_cb(p_cb, rcb_idx)) != BTA_GATTS_INVALID_APP) {
324 /* create the service now */
325 service_id = GATTS_CreateService (p_cb->rcb[rcb_idx].gatt_if,
326 &p_msg->api_create_svc.service_uuid,
327 p_msg->api_create_svc.inst,
328 p_msg->api_create_svc.num_handle,
329 p_msg->api_create_svc.is_pri);
330
331 if (service_id != 0) {
332 memcpy(&p_cb->srvc_cb[srvc_idx].service_uuid,
333 &p_msg->api_create_svc.service_uuid, sizeof(tBT_UUID));
334 p_cb->srvc_cb[srvc_idx].service_id = service_id;
335 p_cb->srvc_cb[srvc_idx].inst_num = p_msg->api_create_svc.inst;
336 p_cb->srvc_cb[srvc_idx].idx = srvc_idx;
337
338 cb_data.create.status = BTA_GATT_OK;
339 cb_data.create.service_id = service_id;
340
341 cb_data.create.is_primary = p_msg->api_create_svc.is_pri;
342
343 cb_data.create.server_if = p_cb->rcb[rcb_idx].gatt_if;
344 } else {
345 cb_data.status = BTA_GATT_ERROR;
346 memset(&p_cb->srvc_cb[srvc_idx], 0, sizeof(tBTA_GATTS_SRVC_CB));
347 APPL_TRACE_ERROR("service creation failed.");
348 }
349
350 memcpy(&cb_data.create.uuid, &p_msg->api_create_svc.service_uuid, sizeof(tBT_UUID));
351 cb_data.create.svc_instance = p_msg->api_create_svc.inst;
352
353 }
354 if (p_cb->rcb[rcb_idx].p_cback) {
355 (* p_cb->rcb[rcb_idx].p_cback)(BTA_GATTS_CREATE_EVT, &cb_data);
356 }
357 } else { /* application not registered */
358 APPL_TRACE_ERROR("Application not registered");
359 }
360 }
361 /*******************************************************************************
362 **
363 ** Function bta_gatts_add_include_srvc
364 **
365 ** Description action function to add an included service.
366 **
367 ** Returns none.
368 **
369 *******************************************************************************/
bta_gatts_add_include_srvc(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA * p_msg)370 void bta_gatts_add_include_srvc(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg)
371 {
372 tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
373 UINT16 attr_id = 0;
374 tBTA_GATTS cb_data;
375
376 attr_id = GATTS_AddIncludeService(p_msg->api_add_incl_srvc.hdr.layer_specific,
377 p_msg->api_add_incl_srvc.included_service_id);
378
379 cb_data.add_result.server_if = p_rcb->gatt_if;
380 cb_data.add_result.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
381 cb_data.add_result.attr_id = attr_id;
382
383 if (attr_id) {
384 cb_data.add_result.status = BTA_GATT_OK;
385 } else {
386 cb_data.add_result.status = BTA_GATT_ERROR;
387 }
388
389 if (p_rcb->p_cback) {
390 (*p_rcb->p_cback)(BTA_GATTS_ADD_INCL_SRVC_EVT, &cb_data);
391 }
392 }
393 /*******************************************************************************
394 **
395 ** Function bta_gatts_add_char
396 **
397 ** Description action function to add characteristic.
398 **
399 ** Returns none.
400 **
401 *******************************************************************************/
bta_gatts_add_char(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA * p_msg)402 void bta_gatts_add_char(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg)
403 {
404 tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
405 UINT16 attr_id = 0;
406 tBTA_GATTS cb_data;
407
408 tGATT_ATTR_VAL *p_attr_val = NULL;
409 tGATTS_ATTR_CONTROL *p_control = NULL;
410
411 if(p_msg->api_add_char.attr_val.attr_max_len != 0){
412 p_attr_val = &p_msg->api_add_char.attr_val;
413 }
414
415 if(p_msg->api_add_char.control.auto_rsp != 0){
416 p_control = &p_msg->api_add_char.control;
417 }
418
419
420 attr_id = GATTS_AddCharacteristic(p_msg->api_add_char.hdr.layer_specific,
421 &p_msg->api_add_char.char_uuid,
422 p_msg->api_add_char.perm,
423 p_msg->api_add_char.property, p_attr_val, p_control);
424 cb_data.add_result.server_if = p_rcb->gatt_if;
425 cb_data.add_result.service_id = p_msg->api_add_char.hdr.layer_specific;
426 cb_data.add_result.attr_id = attr_id;
427 // btla-specific ++
428 memcpy(&cb_data.add_result.char_uuid, &p_msg->api_add_char.char_uuid, sizeof(tBT_UUID));
429 // btla-specific --
430
431 if (attr_id) {
432 cb_data.add_result.status = BTA_GATT_OK;
433 } else {
434 cb_data.add_result.status = BTA_GATT_ERROR;
435 }
436 if((p_attr_val != NULL) && (p_attr_val->attr_val != NULL)){
437 osi_free(p_attr_val->attr_val);
438 }
439
440 if (p_rcb->p_cback) {
441 (*p_rcb->p_cback)(BTA_GATTS_ADD_CHAR_EVT, &cb_data);
442 }
443 }
444
445 /*******************************************************************************
446 **
447 ** Function bta_gatts_add_char_descr
448 **
449 ** Description action function to add characteristic descriptor.
450 **
451 ** Returns none.
452 **
453 *******************************************************************************/
bta_gatts_add_char_descr(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA * p_msg)454 void bta_gatts_add_char_descr(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg)
455 {
456 tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
457 UINT16 attr_id = 0;
458 tBTA_GATTS cb_data;
459 tGATT_ATTR_VAL *p_attr_val = NULL;
460 tGATTS_ATTR_CONTROL *p_control = NULL;
461
462 if (p_msg->api_add_char_descr.attr_val.attr_max_len != 0) {
463 p_attr_val = &p_msg->api_add_char_descr.attr_val;
464 }
465
466 if (p_msg->api_add_char_descr.control.auto_rsp != 0) {
467 p_control = &p_msg->api_add_char_descr.control;
468 }
469 attr_id = GATTS_AddCharDescriptor(p_msg->api_add_char_descr.hdr.layer_specific,
470 p_msg->api_add_char_descr.perm,
471 &p_msg->api_add_char_descr.descr_uuid, p_attr_val,
472 p_control);
473
474 cb_data.add_result.server_if = p_rcb->gatt_if;
475 cb_data.add_result.service_id = p_msg->api_add_char_descr.hdr.layer_specific;
476 cb_data.add_result.attr_id = attr_id;
477 // btla-specific ++
478 memcpy(&cb_data.add_result.char_uuid, &p_msg->api_add_char_descr.descr_uuid, sizeof(tBT_UUID));
479 // btla-specific --
480
481 if (attr_id) {
482 cb_data.add_result.status = BTA_GATT_OK;
483 } else {
484 cb_data.add_result.status = BTA_GATT_ERROR;
485 }
486 if((p_attr_val != NULL) && (p_attr_val->attr_val != NULL)){
487 osi_free(p_attr_val->attr_val);
488 }
489
490 if (p_rcb->p_cback) {
491 (*p_rcb->p_cback)(BTA_GATTS_ADD_CHAR_DESCR_EVT, &cb_data);
492 }
493
494 }
495
496 /*******************************************************************************
497 **
498 ** Function bta_gatts_add_char_descr
499 **
500 ** Description action function to add characteristic descriptor.
501 **
502 ** Returns none.
503 **
504 *******************************************************************************/
bta_gatts_set_attr_value(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA * p_msg)505 void bta_gatts_set_attr_value(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg)
506 {
507 tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
508 UINT16 service_id = p_srvc_cb->service_id;
509 tBTA_GATTS cb_data;
510 tBTA_GATT_STATUS gatts_status;
511 gatts_status = GATTS_SetAttributeValue(p_msg->api_set_val.hdr.layer_specific,
512 p_msg->api_set_val.length,
513 p_msg->api_set_val.value);
514
515 cb_data.attr_val.server_if = p_rcb->gatt_if;
516 cb_data.attr_val.service_id = service_id;
517 cb_data.attr_val.attr_id = p_msg->api_set_val.hdr.layer_specific;
518 cb_data.attr_val.status = gatts_status;
519
520 if (p_msg->api_set_val.value != NULL){
521 osi_free(p_msg->api_set_val.value);
522 }
523
524 if (p_rcb->p_cback) {
525 (*p_rcb->p_cback)(BTA_GATTS_SET_ATTR_VAL_EVT, &cb_data);
526 }
527 }
528
bta_gatts_get_attr_value(UINT16 attr_handle,UINT16 * length,UINT8 ** value)529 tGATT_STATUS bta_gatts_get_attr_value(UINT16 attr_handle, UINT16 *length, UINT8 **value)
530 {
531
532 return GATTS_GetAttributeValue(attr_handle, length, value);
533 }
534
535 /*******************************************************************************
536 **
537 ** Function bta_gatts_delete_service
538 **
539 ** Description action function to delete a service.
540 **
541 ** Returns none.
542 **
543 *******************************************************************************/
bta_gatts_delete_service(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA * p_msg)544 void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg)
545 {
546 tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
547 tBTA_GATTS cb_data;
548
549 cb_data.srvc_oper.server_if = p_rcb->gatt_if;
550 cb_data.srvc_oper.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
551
552 if (GATTS_DeleteService(p_rcb->gatt_if,
553 &p_srvc_cb->service_uuid,
554 p_srvc_cb->inst_num)) {
555 cb_data.srvc_oper.status = BTA_GATT_OK;
556 memset(p_srvc_cb, 0, sizeof(tBTA_GATTS_SRVC_CB));
557 } else {
558 cb_data.srvc_oper.status = BTA_GATT_ERROR;
559 }
560
561 if (p_rcb->p_cback) {
562 (*p_rcb->p_cback)(BTA_GATTS_DELELTE_EVT, &cb_data);
563 }
564
565 }
566 /*******************************************************************************
567 **
568 ** Function bta_gatts_start_service
569 **
570 ** Description action function to start a service.
571 **
572 ** Returns none.
573 **
574 *******************************************************************************/
bta_gatts_start_service(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA * p_msg)575 void bta_gatts_start_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg)
576 {
577 tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
578 tBTA_GATTS cb_data;
579
580 cb_data.srvc_oper.server_if = p_rcb->gatt_if;
581 cb_data.srvc_oper.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
582
583 if (GATTS_StartService(p_rcb->gatt_if,
584 p_srvc_cb->service_id,
585 p_msg->api_start.transport) == GATT_SUCCESS) {
586 APPL_TRACE_DEBUG("bta_gatts_start_service service_id= %d", p_srvc_cb->service_id);
587 cb_data.srvc_oper.status = BTA_GATT_OK;
588 } else {
589 cb_data.srvc_oper.status = BTA_GATT_ERROR;
590 }
591
592 if (p_rcb->p_cback) {
593 (*p_rcb->p_cback)(BTA_GATTS_START_EVT, &cb_data);
594 }
595
596 }
597 /*******************************************************************************
598 **
599 ** Function bta_gatts_stop_service
600 **
601 ** Description action function to stop a service.
602 **
603 ** Returns none.
604 **
605 *******************************************************************************/
bta_gatts_stop_service(tBTA_GATTS_SRVC_CB * p_srvc_cb,tBTA_GATTS_DATA * p_msg)606 void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB *p_srvc_cb, tBTA_GATTS_DATA *p_msg)
607 {
608 tBTA_GATTS_RCB *p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
609 tBTA_GATTS cb_data;
610 UNUSED(p_msg);
611
612 GATTS_StopService(p_srvc_cb->service_id);
613 cb_data.srvc_oper.server_if = p_rcb->gatt_if;
614 cb_data.srvc_oper.service_id = p_srvc_cb->service_id;
615 cb_data.srvc_oper.status = BTA_GATT_OK;
616 APPL_TRACE_DEBUG("bta_gatts_stop_service service_id= %d", p_srvc_cb->service_id);
617
618 if (p_rcb->p_cback) {
619 (*p_rcb->p_cback)(BTA_GATTS_STOP_EVT, &cb_data);
620 }
621
622 }
623 /*******************************************************************************
624 **
625 ** Function bta_gatts_send_rsp
626 **
627 ** Description GATTS send response.
628 **
629 ** Returns none.
630 **
631 *******************************************************************************/
bta_gatts_send_rsp(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)632 void bta_gatts_send_rsp (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
633 {
634 UNUSED(p_cb);
635
636 if (GATTS_SendRsp (p_msg->api_rsp.hdr.layer_specific,
637 p_msg->api_rsp.trans_id,
638 p_msg->api_rsp.status,
639 (tGATTS_RSP *)p_msg->api_rsp.p_rsp) != GATT_SUCCESS) {
640 APPL_TRACE_ERROR("Sending response failed\n");
641 }
642
643 }
644 /*******************************************************************************
645 **
646 ** Function bta_gatts_indicate_handle
647 **
648 ** Description GATTS send handle value indication or notification.
649 **
650 ** Returns none.
651 **
652 *******************************************************************************/
bta_gatts_indicate_handle(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)653 void bta_gatts_indicate_handle (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
654 {
655 tBTA_GATTS_SRVC_CB *p_srvc_cb;
656 tBTA_GATTS_RCB *p_rcb = NULL;
657 tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
658 tGATT_IF gatt_if;
659 BD_ADDR remote_bda;
660 tBTA_TRANSPORT transport;
661 tBTA_GATTS cb_data;
662
663 p_srvc_cb = bta_gatts_find_srvc_cb_by_attr_id (p_cb, p_msg->api_indicate.attr_id);
664
665 if (p_srvc_cb ) {
666 if (GATT_GetConnectionInfor(p_msg->api_indicate.hdr.layer_specific,
667 &gatt_if, remote_bda, &transport)) {
668 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
669
670 if (p_msg->api_indicate.need_confirm) {
671
672 status = GATTS_HandleValueIndication (p_msg->api_indicate.hdr.layer_specific,
673 p_msg->api_indicate.attr_id,
674 p_msg->api_indicate.len,
675 p_msg->api_indicate.value);
676 } else {
677 l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTU_NUM, NULL);
678 status = GATTS_HandleValueNotification (p_msg->api_indicate.hdr.layer_specific,
679 p_msg->api_indicate.attr_id,
680 p_msg->api_indicate.len,
681 p_msg->api_indicate.value);
682 }
683
684 /* if over BR_EDR, inform PM for mode change */
685 if (transport == BTA_TRANSPORT_BR_EDR) {
686 bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
687 bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
688 }
689 } else {
690 APPL_TRACE_ERROR("Unknown connection ID: %d fail sending notification",
691 p_msg->api_indicate.hdr.layer_specific);
692 }
693
694 if ((status != GATT_SUCCESS || !p_msg->api_indicate.need_confirm) &&
695 p_rcb && p_cb->rcb[p_srvc_cb->rcb_idx].p_cback) {
696 cb_data.req_data.status = status;
697 cb_data.req_data.conn_id = p_msg->api_indicate.hdr.layer_specific;
698 cb_data.req_data.value = NULL;
699 cb_data.req_data.data_len = 0;
700 cb_data.req_data.handle = p_msg->api_indicate.attr_id;
701
702 if (p_msg->api_indicate.value && (p_msg->api_indicate.len > 0)) {
703 cb_data.req_data.value = (uint8_t *) osi_malloc(p_msg->api_indicate.len);
704 if (cb_data.req_data.value != NULL) {
705 memset(cb_data.req_data.value, 0, p_msg->api_indicate.len);
706 cb_data.req_data.data_len = p_msg->api_indicate.len;
707 memcpy(cb_data.req_data.value, p_msg->api_indicate.value, p_msg->api_indicate.len);
708 } else {
709 APPL_TRACE_ERROR("%s, malloc failed", __func__);
710 }
711 } else {
712 if (p_msg->api_indicate.value) {
713 APPL_TRACE_ERROR("%s, incorrect length", __func__);
714 }
715 }
716 (*p_rcb->p_cback)(BTA_GATTS_CONF_EVT, &cb_data);
717 if (cb_data.req_data.value != NULL) {
718 osi_free(cb_data.req_data.value);
719 cb_data.req_data.value = NULL;
720 }
721 }
722 } else {
723 APPL_TRACE_ERROR("Not an registered servce attribute ID: 0x%04x",
724 p_msg->api_indicate.attr_id);
725 }
726 }
727
728
729 /*******************************************************************************
730 **
731 ** Function bta_gatts_open
732 **
733 ** Description
734 **
735 ** Returns none.
736 **
737 *******************************************************************************/
bta_gatts_open(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)738 void bta_gatts_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
739 {
740 tBTA_GATTS_RCB *p_rcb = NULL;
741 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
742 UINT16 conn_id;
743 tBTA_GATTS_OPEN open;
744 UNUSED(p_cb);
745
746 if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if)) != NULL) {
747 /* should always get the connection ID */
748 if (GATT_Connect(p_rcb->gatt_if, p_msg->api_open.remote_bda, BLE_ADDR_UNKNOWN_TYPE,
749 p_msg->api_open.is_direct, p_msg->api_open.transport, FALSE)) {
750 status = BTA_GATT_OK;
751
752 if (GATT_GetConnIdIfConnected(p_rcb->gatt_if, p_msg->api_open.remote_bda,
753 &conn_id, p_msg->api_open.transport)) {
754 status = BTA_GATT_ALREADY_OPEN;
755 }
756 }
757 } else {
758 APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_open.server_if);
759 }
760
761 if (p_rcb && p_rcb->p_cback) {
762 open.status = status;
763 open.server_if = p_msg->api_open.server_if;
764 (*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, (tBTA_GATTS *)&open);
765 }
766
767 }
768 /*******************************************************************************
769 **
770 ** Function bta_gatts_cancel_open
771 **
772 ** Description
773 **
774 ** Returns none.
775 **
776 *******************************************************************************/
bta_gatts_cancel_open(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)777 void bta_gatts_cancel_open (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
778 {
779 tBTA_GATTS_RCB *p_rcb;
780 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
781 tBTA_GATTS_CANCEL_OPEN cancel_open;
782 UNUSED(p_cb);
783
784 if ((p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if)) != NULL) {
785 if (!GATT_CancelConnect(p_rcb->gatt_if, p_msg->api_cancel_open.remote_bda,
786 p_msg->api_cancel_open.is_direct)) {
787 APPL_TRACE_ERROR("bta_gatts_cancel_open failed for open request");
788 } else {
789 status = BTA_GATT_OK;
790 }
791 } else {
792 APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_cancel_open.server_if);
793 }
794
795 if (p_rcb && p_rcb->p_cback) {
796 cancel_open.status = status;
797 cancel_open.server_if = p_msg->api_cancel_open.server_if;
798 (*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, (tBTA_GATTS *)&cancel_open);
799
800 }
801 }
802 /*******************************************************************************
803 **
804 ** Function bta_gatts_close
805 **
806 ** Description
807 **
808 ** Returns none.
809 **
810 *******************************************************************************/
bta_gatts_close(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)811 void bta_gatts_close (tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
812 {
813 tBTA_GATTS_RCB *p_rcb;
814 tBTA_GATT_STATUS status = BTA_GATT_ERROR;
815 tGATT_IF gatt_if;
816 BD_ADDR remote_bda;
817 tBTA_GATT_TRANSPORT transport;
818 tBTA_GATTS_CLOSE close;
819 UNUSED(p_cb);
820
821 if (GATT_GetConnectionInfor(p_msg->hdr.layer_specific, &gatt_if, remote_bda, &transport)) {
822 if (GATT_Disconnect(p_msg->hdr.layer_specific) != GATT_SUCCESS) {
823 APPL_TRACE_ERROR("bta_gatts_close fail conn_id=%d", p_msg->hdr.layer_specific);
824 } else {
825 status = BTA_GATT_OK;
826 }
827
828 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
829
830 if (p_rcb && p_rcb->p_cback) {
831 if (transport == BTA_TRANSPORT_BR_EDR) {
832 bta_sys_conn_close( BTA_ID_GATTS , BTA_ALL_APP_ID, remote_bda);
833 }
834
835 close.status = status;
836 close.conn_id = p_msg->hdr.layer_specific;
837 (*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, (tBTA_GATTS *)&close);
838 }
839 } else {
840 APPL_TRACE_ERROR("Unknown connection ID: %d", p_msg->hdr.layer_specific);
841 }
842
843 }
844
845 /*******************************************************************************
846 **
847 ** Function bta_gatts_send_service_change_indication
848 **
849 ** Description gatts send service change indication
850 **
851 ** Returns none.
852 **
853 *******************************************************************************/
bta_gatts_send_service_change_indication(tBTA_GATTS_DATA * p_msg)854 void bta_gatts_send_service_change_indication (tBTA_GATTS_DATA *p_msg)
855 {
856 tBTA_GATTS_RCB *p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_send_service_change.server_if);
857 tBTA_GATTS_SERVICE_CHANGE service_change;
858 tBTA_GATT_STATUS status = BTA_GATT_OK;
859 UINT16 addr[BD_ADDR_LEN] = {0};
860 if(memcmp(p_msg->api_send_service_change.remote_bda, addr, BD_ADDR_LEN) != 0) {
861 BD_ADDR bd_addr;
862 memcpy(bd_addr, p_msg->api_send_service_change.remote_bda, BD_ADDR_LEN);
863 status = GATT_SendServiceChangeIndication(bd_addr);
864 } else {
865 status = GATT_SendServiceChangeIndication(NULL);
866 }
867 if (p_rcb && p_rcb->p_cback) {
868 service_change.status = status;
869 service_change.server_if = p_msg->api_send_service_change.server_if;
870 (*p_rcb->p_cback)(BTA_GATTS_SEND_SERVICE_CHANGE_EVT, (tBTA_GATTS *)&service_change);
871 }
872 }
873
874 /*******************************************************************************
875 **
876 ** Function bta_gatts_listen
877 **
878 ** Description Start or stop listening for LE connection on a GATT server
879 **
880 ** Returns none.
881 **
882 *******************************************************************************/
bta_gatts_listen(tBTA_GATTS_CB * p_cb,tBTA_GATTS_DATA * p_msg)883 void bta_gatts_listen(tBTA_GATTS_CB *p_cb, tBTA_GATTS_DATA *p_msg)
884 {
885 tBTA_GATTS_RCB *p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_listen.server_if);
886 tBTA_GATTS cb_data;
887 UNUSED(p_cb);
888
889 cb_data.reg_oper.status = BTA_GATT_OK;
890 cb_data.reg_oper.server_if = p_msg->api_listen.server_if;
891
892 if (p_rcb == NULL) {
893 APPL_TRACE_ERROR("Unknown GATTS application");
894 return;
895 }
896
897 if (!GATT_Listen(p_msg->api_listen.server_if,
898 p_msg->api_listen.start,
899 p_msg->api_listen.remote_bda)) {
900 cb_data.status = BTA_GATT_ERROR;
901 APPL_TRACE_ERROR("bta_gatts_listen Listen failed");
902 }
903
904 if (p_rcb->p_cback) {
905 (*p_rcb->p_cback)(BTA_GATTS_LISTEN_EVT, &cb_data);
906 }
907 }
908
909 /*******************************************************************************
910 **
911 ** Function bta_gatts_request_cback
912 **
913 ** Description GATTS attribute request callback.
914 **
915 ** Returns none.
916 **
917 *******************************************************************************/
bta_gatts_send_request_cback(UINT16 conn_id,UINT32 trans_id,tGATTS_REQ_TYPE req_type,tGATTS_DATA * p_data)918 static void bta_gatts_send_request_cback (UINT16 conn_id,
919 UINT32 trans_id,
920 tGATTS_REQ_TYPE req_type, tGATTS_DATA *p_data)
921 {
922 tBTA_GATTS cb_data;
923 tBTA_GATTS_RCB *p_rcb;
924 tGATT_IF gatt_if;
925 tBTA_GATT_TRANSPORT transport;
926
927 memset(&cb_data, 0 , sizeof(tBTA_GATTS));
928
929 if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda, &transport)) {
930 p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
931
932 APPL_TRACE_DEBUG ("bta_gatts_send_request_cback conn_id=%d trans_id=%d req_type=%d",
933 conn_id, trans_id, req_type);
934
935 if (p_rcb && p_rcb->p_cback) {
936 /* if over BR_EDR, inform PM for mode change */
937 if (transport == BTA_TRANSPORT_BR_EDR) {
938 bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
939 bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
940 }
941
942 cb_data.req_data.conn_id = conn_id;
943 cb_data.req_data.trans_id = trans_id;
944 cb_data.req_data.p_data = (tBTA_GATTS_REQ_DATA *)p_data;
945
946 if(req_type == BTA_GATTS_CONF_EVT) {
947 cb_data.req_data.handle = p_data->handle;
948 }
949 (*p_rcb->p_cback)(req_type, &cb_data);
950 } else {
951 APPL_TRACE_ERROR("connection request on gatt_if[%d] is not interested", gatt_if);
952 }
953 } else {
954 APPL_TRACE_ERROR("request received on unknown connection ID: %d", conn_id);
955 }
956 }
957
958 /*******************************************************************************
959 **
960 ** Function bta_gatts_conn_cback
961 **
962 ** Description connection callback.
963 **
964 ** Returns none.
965 **
966 *******************************************************************************/
bta_gatts_conn_cback(tGATT_IF gatt_if,BD_ADDR bda,UINT16 conn_id,BOOLEAN connected,tGATT_DISCONN_REASON reason,tGATT_TRANSPORT transport)967 static void bta_gatts_conn_cback (tGATT_IF gatt_if, BD_ADDR bda, UINT16 conn_id,
968 BOOLEAN connected, tGATT_DISCONN_REASON reason,
969 tGATT_TRANSPORT transport)
970 {
971 tBTA_GATTS cb_data = {0};
972 UINT8 evt = connected ? BTA_GATTS_CONNECT_EVT : BTA_GATTS_DISCONNECT_EVT;
973 tBTA_GATTS_RCB *p_reg;
974
975 APPL_TRACE_DEBUG ("bta_gatts_conn_cback gatt_if=%d conn_id=%d connected=%d reason = 0x%04d",
976 gatt_if, conn_id, connected, reason);
977 APPL_TRACE_DEBUG("bta_gatts_conn_cback bda :%02x-%02x-%02x-%02x-%02x-%02x ",
978 bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
979
980 bt_bdaddr_t bdaddr;
981 bdcpy(bdaddr.address, bda);
982 /*
983 if (connected)
984 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
985 else
986 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
987 */
988 p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
989
990 if (p_reg && p_reg->p_cback) {
991 /* there is no RM for GATT */
992 if (transport == BTA_TRANSPORT_BR_EDR) {
993 if (connected) {
994 bta_sys_conn_open(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
995 } else {
996 bta_sys_conn_close( BTA_ID_GATTS , BTA_ALL_APP_ID, bda);
997 }
998 }
999 if(evt == BTA_GATTS_CONNECT_EVT) {
1000 tL2C_LCB *p_lcb = l2cu_find_lcb_by_bd_addr(bda, BT_TRANSPORT_LE);
1001 if(p_lcb != NULL) {
1002 cb_data.conn.conn_params.interval = p_lcb->current_used_conn_interval;
1003 cb_data.conn.conn_params.latency = p_lcb->current_used_conn_latency;
1004 cb_data.conn.conn_params.timeout = p_lcb->current_used_conn_timeout;
1005 cb_data.conn.link_role = p_lcb->link_role;
1006 }else {
1007 APPL_TRACE_WARNING("%s not found connection parameters of the device ", __func__);
1008 }
1009 }
1010 cb_data.conn.conn_id = conn_id;
1011 cb_data.conn.server_if = gatt_if;
1012 cb_data.conn.reason = reason;
1013 cb_data.conn.transport = transport;
1014 memcpy(cb_data.conn.remote_bda, bda, BD_ADDR_LEN);
1015 (*p_reg->p_cback)(evt, &cb_data);
1016 } else {
1017 APPL_TRACE_ERROR("bta_gatts_conn_cback server_if=%d not found", gatt_if);
1018 }
1019 }
1020
1021 /*******************************************************************************
1022 **
1023 ** Function bta_gatts_cong_cback
1024 **
1025 ** Description congestion callback.
1026 **
1027 ** Returns none.
1028 **
1029 *******************************************************************************/
bta_gatts_cong_cback(UINT16 conn_id,BOOLEAN congested)1030 static void bta_gatts_cong_cback (UINT16 conn_id, BOOLEAN congested)
1031 {
1032 tBTA_GATTS cb_data;
1033 cb_data.congest.conn_id = conn_id;
1034 cb_data.congest.congested = congested;
1035 btc_congest_callback(&cb_data);
1036 }
1037 #endif /* GATTS_INCLUDED */
1038