-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
bta_gattc_act.c
2453 lines (2198 loc) · 88.2 KB
/
bta_gattc_act.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Copyright (C) 2003-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the GATT client action functions for the state
* machine.
*
******************************************************************************/
#define LOG_TAG "bt_bta_gattc"
#include "common/bt_target.h"
#include "bta/utl.h"
#include "bta/bta_sys.h"
#include "bta_gattc_int.h"
#include "stack/l2c_api.h"
#include "l2c_int.h"
#include "gatt_int.h"
#include "osi/allocator.h"
#include "osi/mutex.h"
#include "bta_hh_int.h"
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
#include "bta_hh_int.h"
#endif
// #include "btif/include/btif_debug_conn.h"
#include <string.h>
// #include "osi/include/log.h"
#if GATTC_INCLUDED == TRUE && BLE_INCLUDED == TRUE
/*****************************************************************************
** Constants
*****************************************************************************/
static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
BOOLEAN connected, tGATT_DISCONN_REASON reason,
tBT_TRANSPORT transport);
static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
tGATT_CL_COMPLETE *p_data);
static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
tBTA_GATT_STATUS status,
tGATT_CL_COMPLETE *p_data);
static void bta_gattc_pop_command_to_send(tBTA_GATTC_CLCB *p_clcb);
static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
static void bta_gattc_req_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type, tGATTS_DATA *p_data);
static tBTA_GATTC_FIND_SERVICE_CB bta_gattc_register_service_change_notify(UINT16 conn_id, BD_ADDR remote_bda);
extern void btc_gattc_congest_callback(tBTA_GATTC *param);
static const tGATT_CBACK bta_gattc_cl_cback = {
bta_gattc_conn_cback,
bta_gattc_cmpl_cback,
bta_gattc_disc_res_cback,
bta_gattc_disc_cmpl_cback,
bta_gattc_req_cback,
bta_gattc_enc_cmpl_cback,
bta_gattc_cong_cback
};
/* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
static const UINT16 bta_gattc_opcode_to_int_evt[] = {
BTA_GATTC_API_READ_EVT,
BTA_GATTC_API_WRITE_EVT,
BTA_GATTC_API_EXEC_EVT,
BTA_GATTC_API_CFG_MTU_EVT,
BTA_GATTC_API_READ_MULTI_EVT
};
#if (BT_TRACE_VERBOSE == TRUE)
static const char *bta_gattc_op_code_name[] = {
"Unknown",
"Discovery",
"Read",
"Write",
"Exec",
"Config",
"Notification",
"Indication"
};
#endif
/*****************************************************************************
** Action Functions
*****************************************************************************/
void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status);
/*******************************************************************************
**
** Function bta_gattc_enable
**
** Description Enables GATTC module
**
**
** Returns void
**
*******************************************************************************/
static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
{
APPL_TRACE_DEBUG("bta_gattc_enable");
if (p_cb->state == BTA_GATTC_STATE_DISABLED) {
/* initialize control block */
memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
p_cb->state = BTA_GATTC_STATE_ENABLED;
} else {
APPL_TRACE_DEBUG("GATTC is already enabled");
}
}
/*******************************************************************************
**
** Function bta_gattc_disable
**
** Description Disable GATTC module by cleaning up all active connections
** and deregister all application.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
{
UINT8 i;
APPL_TRACE_DEBUG("bta_gattc_disable");
if (p_cb->state != BTA_GATTC_STATE_ENABLED) {
APPL_TRACE_ERROR("not enabled or disable in pogress");
return;
}
for (i = 0; i < BTA_GATTC_CL_MAX; i ++) {
if (p_cb->cl_rcb[i].in_use) {
p_cb->state = BTA_GATTC_STATE_DISABLING;
/* don't deregister HH GATT IF */
/* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
#endif
bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
}
#endif
}
}
/* no registered apps, indicate disable completed */
if (p_cb->state != BTA_GATTC_STATE_DISABLING) {
p_cb->state = BTA_GATTC_STATE_DISABLED;
memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
}
}
/*******************************************************************************
**
** Function bta_gattc_register
**
** Description Register a GATT client application with BTA.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC cb_data;
UINT8 i;
tBT_UUID *p_app_uuid = &p_data->api_reg.app_uuid;
tBTA_GATTC_INT_START_IF *p_buf;
tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
APPL_TRACE_DEBUG("bta_gattc_register state %d\n", p_cb->state);
memset(&cb_data, 0, sizeof(cb_data));
cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
/* check if GATTC module is already enabled . Else enable */
if (p_cb->state == BTA_GATTC_STATE_DISABLED) {
bta_gattc_enable (p_cb);
}
/* todo need to check duplicate uuid */
for (i = 0; i < BTA_GATTC_CL_MAX; i ++) {
if (!p_cb->cl_rcb[i].in_use) {
if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0) {
APPL_TRACE_ERROR("Register with GATT stack failed.\n");
status = BTA_GATT_ERROR;
} else {
p_cb->cl_rcb[i].in_use = TRUE;
p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback;
memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
/* BTA use the same client interface as BTE GATT statck */
cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if;
if ((p_buf = (tBTA_GATTC_INT_START_IF *) osi_malloc(sizeof(tBTA_GATTC_INT_START_IF))) != NULL) {
p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT;
p_buf->client_if = p_cb->cl_rcb[i].client_if;
APPL_TRACE_DEBUG("GATTC getbuf sucess.\n");
bta_sys_sendmsg(p_buf);
status = BTA_GATT_OK;
} else {
GATT_Deregister(p_cb->cl_rcb[i].client_if);
status = BTA_GATT_NO_RESOURCES;
memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB));
}
break;
}
}
}
/* callback with register event */
if (p_data->api_reg.p_cback) {
if (p_app_uuid != NULL) {
memcpy(&(cb_data.reg_oper.app_uuid), p_app_uuid, sizeof(tBT_UUID));
}
cb_data.reg_oper.status = status;
(*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC *)&cb_data);
}
}
/*******************************************************************************
**
** Function bta_gattc_start_if
**
** Description start an application interface.
**
** Returns none.
**
*******************************************************************************/
void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
{
UNUSED(p_cb);
if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) != NULL ) {
GATT_StartIf(p_msg->int_start_if.client_if);
} else {
APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d", p_msg->int_start_if.client_if );
}
}
/*******************************************************************************
**
** Function bta_gattc_deregister
**
** Description De-Register a GATT client application with BTA.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg)
{
UINT8 i;
BT_HDR buf;
if (p_clreg != NULL) {
/* remove bg connection associated with this rcb */
for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++) {
if (p_cb->bg_track[i].in_use) {
if (p_cb->bg_track[i].cif_mask & (1 << (p_clreg->client_if - 1))) {
bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE);
GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
}
if (p_cb->bg_track[i].cif_adv_mask & (1 << (p_clreg->client_if - 1))) {
bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
}
}
}
if (p_clreg->num_clcb > 0) {
/* close all CLCB related to this app */
for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) {
if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg)) {
p_clreg->dereg_pending = TRUE;
buf.event = BTA_GATTC_API_CLOSE_EVT;
buf.layer_specific = p_cb->clcb[i].bta_conn_id;
bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf) ;
}
}
} else {
bta_gattc_deregister_cmpl(p_clreg);
}
} else {
APPL_TRACE_ERROR("Deregister Failed unknown client cif");
#if defined(BTA_HH_INCLUDED) && (BTA_HH_INCLUDED == TRUE)
bta_hh_cleanup_disable(BTA_HH_OK);
#endif
}
}
/*******************************************************************************
**
** Function bta_gattc_process_api_open
**
** Description process connect API request.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
{
UINT16 event = ((BT_HDR *)p_msg)->event;
tBTA_GATTC_CLCB *p_clcb = NULL;
tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
UNUSED(p_cb);
if (p_clreg != NULL) {
if (p_msg->api_conn.is_direct) {
if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
p_msg->api_conn.remote_bda,
p_msg->api_conn.transport)) != NULL) {
bta_gattc_sm_execute(p_clcb, event, p_msg);
} else {
APPL_TRACE_ERROR("No resources to open a new connection.");
bta_gattc_send_open_cback(p_clreg,
BTA_GATT_NO_RESOURCES,
p_msg->api_conn.remote_bda,
BTA_GATT_INVALID_CONN_ID,
p_msg->api_conn.transport, 0);
}
} else {
bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
}
} else {
APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d",
p_msg->api_conn.client_if);
}
}
/*******************************************************************************
**
** Function bta_gattc_process_api_open_cancel
**
** Description process connect API request.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
{
UINT16 event = ((BT_HDR *)p_msg)->event;
tBTA_GATTC_CLCB *p_clcb = NULL;
tBTA_GATTC_RCB *p_clreg;
tBTA_GATTC cb_data;
UNUSED(p_cb);
if (p_msg->api_cancel_conn.is_direct) {
if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if,
p_msg->api_cancel_conn.remote_bda,
BTA_GATT_TRANSPORT_LE)) != NULL) {
bta_gattc_sm_execute(p_clcb, event, p_msg);
} else {
APPL_TRACE_ERROR("No such connection need to be cancelled");
p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
if (p_clreg && p_clreg->p_cback) {
cb_data.status = BTA_GATT_ERROR;
(*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
}
}
} else {
bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
}
}
/*******************************************************************************
**
** Function bta_gattc_process_enc_cmpl
**
** Description process encryption complete message.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
{
tBTA_GATTC_RCB *p_clreg;
tBTA_GATTC cb_data;
UNUSED(p_cb);
p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if);
if (p_clreg && p_clreg->p_cback) {
memset(&cb_data, 0, sizeof(tBTA_GATTC));
cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if;
bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda);
(*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
}
}
/*******************************************************************************
**
** Function bta_gattc_cancel_open_error
**
** Description
**
** Returns void
**
*******************************************************************************/
void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC cb_data;
UNUSED(p_data);
cb_data.status = BTA_GATT_ERROR;
if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback ) {
(*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
}
}
/*******************************************************************************
**
** Function bta_gattc_open_error
**
** Description
**
** Returns void
**
*******************************************************************************/
void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
UNUSED(p_data);
APPL_TRACE_ERROR("Connection already opened. wrong state");
bta_gattc_send_open_cback(p_clcb->p_rcb,
BTA_GATT_OK,
p_clcb->bda,
p_clcb->bta_conn_id,
p_clcb->transport,
0);
}
/*******************************************************************************
**
** Function bta_gattc_open_fail
**
** Description
**
** Returns void
**
*******************************************************************************/
void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
UNUSED(p_data);
bta_gattc_send_open_cback(p_clcb->p_rcb,
BTA_GATT_ERROR,
p_clcb->bda,
p_clcb->bta_conn_id,
p_clcb->transport,
0);
/* open failure, remove clcb */
bta_gattc_clcb_dealloc(p_clcb);
}
/*******************************************************************************
**
** Function bta_gattc_open
**
** Description Process API connection function.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC_DATA gattc_data;
BOOLEAN found_app = FALSE;
tGATT_TCB *p_tcb;
if (!p_clcb || !p_data) {
return;
}
p_tcb = gatt_find_tcb_by_addr(p_data->api_conn.remote_bda, BT_TRANSPORT_LE);
if(p_tcb) {
found_app = gatt_find_specific_app_in_hold_link(p_tcb, p_clcb->p_rcb->client_if);
}
/* open/hold a connection */
if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda, p_data->api_conn.remote_addr_type,
TRUE, p_data->api_conn.transport, p_data->api_conn.is_aux)) {
APPL_TRACE_ERROR("Connection open failure");
bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
} else {
/* a connected remote device */
if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if,
p_data->api_conn.remote_bda,
&p_clcb->bta_conn_id,
p_data->api_conn.transport)) {
gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
gattc_data.int_conn.already_connect = found_app;
bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
}
/* else wait for the callback event */
}
}
/*******************************************************************************
**
** Function bta_gattc_init_bk_conn
**
** Description Process API Open for a background connection
**
** Returns void
**
*******************************************************************************/
void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg)
{
tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
UINT16 conn_id;
tBTA_GATTC_CLCB *p_clcb;
tBTA_GATTC_DATA gattc_data;
if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE)) {
/* always call open to hold a connection */
if (!GATT_Connect(p_data->client_if, p_data->remote_bda,
p_data->remote_addr_type, FALSE,
p_data->transport, p_data->is_aux)) {
#if (!CONFIG_BT_STACK_NO_LOG)
uint8_t *bda = (uint8_t *)p_data->remote_bda;
#endif
status = BTA_GATT_ERROR;
APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
__func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
} else {
status = BTA_GATT_OK;
/* if is a connected remote device */
if (GATT_GetConnIdIfConnected(p_data->client_if,
p_data->remote_bda,
&conn_id,
p_data->transport)) {
if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda,
BTA_GATT_TRANSPORT_LE)) != NULL) {
gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
/* open connection */
bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
status = BTA_GATT_OK;
}
}
}
}
/* open failure, report OPEN_EVT */
if (status != BTA_GATT_OK) {
bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda,
BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0);
}
}
/*******************************************************************************
**
** Function bta_gattc_cancel_bk_conn
**
** Description Process API Cancel Open for a background connection
**
** Returns void
**
*******************************************************************************/
void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data)
{
tBTA_GATTC_RCB *p_clreg;
tBTA_GATTC cb_data;
cb_data.status = BTA_GATT_ERROR;
/* remove the device from the bg connection mask */
if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE)) {
if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE)) {
cb_data.status = BTA_GATT_OK;
} else {
APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed");
}
}
p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
if (p_clreg && p_clreg->p_cback) {
(*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
}
}
/*******************************************************************************
**
** Function bta_gattc_int_cancel_open_ok
**
** Description
**
** Returns void
**
*******************************************************************************/
void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC cb_data;
UNUSED(p_data);
if ( p_clcb->p_rcb->p_cback ) {
cb_data.status = BTA_GATT_OK;
(*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
}
bta_gattc_clcb_dealloc(p_clcb);
}
/*******************************************************************************
**
** Function bta_gattc_cancel_open
**
** Description
**
** Returns void
**
*******************************************************************************/
void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC cb_data;
if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE)) {
bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
} else {
if ( p_clcb->p_rcb->p_cback ) {
cb_data.status = BTA_GATT_ERROR;
(*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
}
}
}
/*******************************************************************************
**
** Function bta_gattc_conn
**
** Description receive connection callback from stack
**
** Returns void
**
*******************************************************************************/
void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC_IF gatt_if;
APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d", p_clcb->p_srcb->state);
if (p_data != NULL) {
APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d", p_data->hdr.layer_specific);
p_clcb->bta_conn_id = p_data->int_conn.hdr.layer_specific;
GATT_GetConnectionInfor(p_data->hdr.layer_specific,
&gatt_if, p_clcb->bda, &p_clcb->transport);
}
p_clcb->p_srcb->connected = TRUE;
if (p_clcb->p_srcb->mtu == 0) {
p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
}
/* start database cache if needed */
if (p_clcb->p_srcb->p_srvc_cache == NULL ||
p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE) {
if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) {
#if (GATTC_CACHE_NVS == TRUE)
p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
if (bta_gattc_cache_load(p_clcb)) {
p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
//register service change
bta_gattc_register_service_change_notify(p_clcb->bta_conn_id, p_clcb->bda);
} else
#endif
{ /* cache is building */
p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
/* cache load failure, start discovery */
bta_gattc_start_discover(p_clcb, NULL);
}
} else { /* cache is building */
p_clcb->state = BTA_GATTC_DISCOVER_ST;
}
} else {
/* a pending service handle change indication */
if (p_clcb->p_srcb->srvc_hdl_chg) {
p_clcb->p_srcb->srvc_hdl_chg = FALSE;
/* start discovery */
bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
}
}
if (p_clcb->p_rcb) {
/* there is no RM for GATT */
if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) {
bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
}
tBTA_GATT_STATUS status = BTA_GATT_OK;
if (p_data && p_data->int_conn.already_connect) {
//clear already_connect
p_data->int_conn.already_connect = FALSE;
status = BTA_GATT_ALREADY_OPEN;
}
bta_gattc_send_open_cback(p_clcb->p_rcb,
status,
p_clcb->bda,
p_clcb->bta_conn_id,
p_clcb->transport,
p_clcb->p_srcb->mtu);
}
}
/*******************************************************************************
**
** Function bta_gattc_conncback
**
** Description receive connection callback from stack
**
** Returns void
**
*******************************************************************************/
void bta_gattc_conncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data)
{
if (p_rcb) {
bta_gattc_send_connect_cback(p_rcb,
p_data->int_conn.remote_bda,
p_data->int_conn.hdr.layer_specific, p_data->int_conn.conn_params, p_data->int_conn.role);
}
}
/*******************************************************************************
**
** Function bta_gattc_disconncback
**
** Description receive disconnection callback from stack
**
** Returns void
**
*******************************************************************************/
void bta_gattc_disconncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data)
{
if (p_rcb) {
// Clear up the notification registration information by BD_ADDR
bta_gattc_clear_notif_registration_by_bda(p_rcb, p_data->int_conn.remote_bda);
bta_gattc_send_disconnect_cback(p_rcb,
p_data->int_conn.reason,
p_data->int_conn.remote_bda,
p_data->int_conn.hdr.layer_specific);
}
}
/*******************************************************************************
**
** Function bta_gattc_close_fail
**
** Description close a connection.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC cb_data;
if ( p_clcb->p_rcb->p_cback ) {
memset(&cb_data, 0, sizeof(tBTA_GATTC));
cb_data.close.client_if = p_clcb->p_rcb->client_if;
cb_data.close.conn_id = p_data->hdr.layer_specific;
bdcpy(cb_data.close.remote_bda, p_clcb->bda);
cb_data.close.status = BTA_GATT_ERROR;
cb_data.close.reason = BTA_GATT_CONN_NONE;
(*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
}
}
/*******************************************************************************
**
** Function bta_gattc_api_close
**
** Description close a GATTC connection.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATTC_CBACK *p_cback = p_clcb->p_rcb->p_cback;
tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb;
tBTA_GATTC cb_data;
APPL_TRACE_DEBUG("bta_gattc_close conn_id=%d", p_clcb->bta_conn_id);
cb_data.close.client_if = p_clcb->p_rcb->client_if;
cb_data.close.conn_id = p_clcb->bta_conn_id;
cb_data.close.reason = p_clcb->reason;
cb_data.close.status = p_clcb->status;
bdcpy(cb_data.close.remote_bda, p_clcb->bda);
if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) {
bta_sys_conn_close( BTA_ID_GATTC , BTA_ALL_APP_ID, p_clcb->bda);
}
bta_gattc_clcb_dealloc(p_clcb);
if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT) {
cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
} else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT) {
cb_data.close.status = BTA_GATT_OK;
cb_data.close.reason = p_data->int_conn.reason;
}
if (p_cback) {
(* p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data);
}
if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending) {
bta_gattc_deregister_cmpl(p_clreg);
}
}
/*******************************************************************************
**
** Function bta_gattc_reset_discover_st
**
** Description when a SRCB finished discovery, tell all related clcb.
**
** Returns None.
**
*******************************************************************************/
void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status)
{
tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
UINT8 i;
for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) {
if (p_cb->clcb[i].p_srcb == p_srcb) {
p_cb->clcb[i].status = status;
bta_gattc_sm_execute(&p_cb->clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT, NULL);
}
}
}
/*******************************************************************************
**
** Function bta_gattc_disc_close
**
** Description close a GATTC connection while in discovery state.
**
** Returns void
**
*******************************************************************************/
void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
APPL_TRACE_DEBUG("%s: Discovery cancel conn_id=%d", __func__,
p_clcb->bta_conn_id);
if (p_clcb->disc_active) {
bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
} else {
p_clcb->state = BTA_GATTC_CONN_ST;
}
// This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
// while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
// connection itself still needs to be closed to resolve the original event.
if (p_clcb->state == BTA_GATTC_CONN_ST) {
APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. "
"Trigger connection close");
bta_gattc_close(p_clcb, p_data);
}
}
/*******************************************************************************
**
** Function bta_gattc_set_discover_st
**
** Description when a SRCB start discovery, tell all related clcb and set
** the state.
**
** Returns None.
**
*******************************************************************************/
void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
{
tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
UINT8 i;
for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) {
if (p_cb->clcb[i].p_srcb == p_srcb) {
p_cb->clcb[i].status = BTA_GATT_OK;
p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
}
}
}
/*******************************************************************************
**
** Function bta_gattc_restart_discover
**
** Description process service change in discovery state, mark up the auto
** update flag and set status to be discovery cancel for current
** discovery.
**
** Returns None.
**
*******************************************************************************/
void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
UNUSED(p_data);
p_clcb->status = BTA_GATT_CANCEL;
p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
}
/*******************************************************************************
**
** Function bta_gattc_cfg_mtu
**
** Description Configure MTU size on the GATT connection.
**
** Returns None.
**
*******************************************************************************/
void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
tBTA_GATT_STATUS status;
if (bta_gattc_enqueue(p_clcb, p_data)) {
status = GATTC_ConfigureMTU (p_clcb->bta_conn_id);
/* if failed, return callback here */
if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) {
/* Dequeue the data, if it was enqueued */
if (p_clcb->p_q_cmd == p_data) {
p_clcb->p_q_cmd = NULL;
bta_gattc_pop_command_to_send(p_clcb);
}
bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, status, NULL);
}
}
}
/*******************************************************************************
**
** Function bta_gattc_start_discover
**
** Description Start a discovery send to server.
**
** Returns None.
**
*******************************************************************************/
void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
{
UNUSED(p_data);
APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
p_clcb->bta_conn_id, p_clcb->p_srcb->state);
if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC) {
/* no pending operation, start discovery right away */
p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
if (p_clcb->p_srcb != NULL) {
/* clear the service change mask */
p_clcb->p_srcb->srvc_hdl_chg = FALSE;
p_clcb->p_srcb->update_count = 0;
p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
#if (BT_MULTI_CONNECTION_ENBALE == FALSE)
if (p_clcb->transport == BTA_TRANSPORT_LE) {
L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
}
#endif
/* set all srcb related clcb into discovery ST */
bta_gattc_set_discover_st(p_clcb->p_srcb);
if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK) {
p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
}
if (p_clcb->status != BTA_GATT_OK) {
APPL_TRACE_ERROR("discovery on server failed");
bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);