-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathbta_jv_act.c
2779 lines (2464 loc) · 91.5 KB
/
bta_jv_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) 2006-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 action functions for BTA JV APIs.
*
******************************************************************************/
#include <pthread.h>
#include <stdlib.h>
#include "osi/allocator.h"
#include "stack/bt_types.h"
#include "bta/utl.h"
#include "bta/bta_sys.h"
#include "bta/bta_api.h"
#include "bta/bta_jv_api.h"
#include "bta_jv_int.h"
#include "bta/bta_jv_co.h"
#include "stack/btm_api.h"
#include "btm_int.h"
#include "stack/sdp_api.h"
#include "stack/l2c_api.h"
#include "stack/port_api.h"
#include <string.h>
#include "stack/rfcdefs.h"
#include "stack/avct_api.h"
#include "stack/avdt_api.h"
#include "stack/gap_api.h"
#include "stack/l2c_api.h"
#if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
/* one of these exists for each client */
struct fc_client {
struct fc_client *next_all_list;
struct fc_client *next_chan_list;
BD_ADDR remote_addr;
uint32_t id;
tBTA_JV_L2CAP_CBACK *p_cback;
void *user_data;
uint16_t handle;
uint16_t chan;
uint8_t sec_id;
unsigned server : 1;
unsigned init_called : 1;
};
/* one of these exists for each channel we're dealing with */
struct fc_channel {
struct fc_channel *next;
struct fc_client *clients;
uint8_t has_server : 1;
uint16_t chan;
};
static struct fc_client *fc_clients;
static struct fc_channel *fc_channels;
static uint32_t fc_next_id;
static pthread_once_t fc_init_once = PTHREAD_ONCE_INIT;
static void fc_init_work(void)
{
fc_clients = NULL;
fc_channels = NULL;
fc_next_id = 0;
//more init here if needed...
}
static void __attribute__((unused)) fc_init(void)
{
pthread_once(&fc_init_once, fc_init_work);
}
static void fcchan_conn_chng_cbk(UINT16 chan, BD_ADDR bd_addr, BOOLEAN connected,
UINT16 reason, tBT_TRANSPORT );
static void fcchan_data_cbk(UINT16 chan, BD_ADDR bd_addr, BT_HDR *p_buf);
extern void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str);
static inline void logu(const char *title, const uint8_t *p_uuid)
{
char uuids[128];
uuid_to_string_legacy((bt_uuid_t *)p_uuid, uuids);
APPL_TRACE_DEBUG("%s: %s", title, uuids);
}
static tBTA_JV_PCB *bta_jv_add_rfc_port(tBTA_JV_RFC_CB *p_cb, tBTA_JV_PCB *p_pcb_open);
static tBTA_JV_STATUS bta_jv_free_set_pm_profile_cb(UINT32 jv_handle);
static void bta_jv_pm_conn_busy(tBTA_JV_PM_CB *p_cb);
static void bta_jv_pm_conn_idle(tBTA_JV_PM_CB *p_cb);
static void bta_jv_pm_state_change(tBTA_JV_PM_CB *p_cb, const tBTA_JV_CONN_STATE state);
tBTA_JV_STATUS bta_jv_set_pm_conn_state(tBTA_JV_PM_CB *p_cb, const tBTA_JV_CONN_STATE
new_st);
/*******************************************************************************
**
** Function bta_jv_alloc_sec_id
**
** Description allocate a security id
**
** Returns
**
*******************************************************************************/
UINT8 bta_jv_alloc_sec_id(void)
{
UINT8 ret = 0;
int i;
for (i = 0; i < BTA_JV_NUM_SERVICE_ID; i++) {
if (0 == bta_jv_cb.sec_id[i]) {
bta_jv_cb.sec_id[i] = BTA_JV_FIRST_SERVICE_ID + i;
ret = bta_jv_cb.sec_id[i];
break;
}
}
return ret;
}
static int get_sec_id_used(void)
{
int i;
int used = 0;
for (i = 0; i < BTA_JV_NUM_SERVICE_ID; i++) {
if (bta_jv_cb.sec_id[i]) {
used++;
}
}
if (used == BTA_JV_NUM_SERVICE_ID)
APPL_TRACE_ERROR("get_sec_id_used, sec id exceeds the limit:%d",
BTA_JV_NUM_SERVICE_ID);
return used;
}
static int get_rfc_cb_used(void)
{
int i;
int used = 0;
for (i = 0; i < BTA_JV_MAX_RFC_CONN; i++) {
if (bta_jv_cb.rfc_cb[i].handle ) {
used++;
}
}
if (used == BTA_JV_MAX_RFC_CONN)
APPL_TRACE_ERROR("get_sec_id_used, rfc ctrl block exceeds the limit:%d",
BTA_JV_MAX_RFC_CONN);
return used;
}
/*******************************************************************************
**
** Function bta_jv_free_sec_id
**
** Description free the given security id
**
** Returns
**
*******************************************************************************/
static void bta_jv_free_sec_id(UINT8 *p_sec_id)
{
UINT8 sec_id = *p_sec_id;
*p_sec_id = 0;
if (sec_id >= BTA_JV_FIRST_SERVICE_ID && sec_id <= BTA_JV_LAST_SERVICE_ID) {
BTM_SecClrService(sec_id);
bta_jv_cb.sec_id[sec_id - BTA_JV_FIRST_SERVICE_ID] = 0;
}
}
/*******************************************************************************
**
** Function bta_jv_alloc_rfc_cb
**
** Description allocate a control block for the given port handle
**
** Returns
**
*******************************************************************************/
tBTA_JV_RFC_CB *bta_jv_alloc_rfc_cb(UINT16 port_handle, tBTA_JV_PCB **pp_pcb)
{
tBTA_JV_RFC_CB *p_cb = NULL;
tBTA_JV_PCB *p_pcb;
int i, j;
for (i = 0; i < BTA_JV_MAX_RFC_CONN; i++) {
if (0 == bta_jv_cb.rfc_cb[i].handle ) {
p_cb = &bta_jv_cb.rfc_cb[i];
/* mask handle to distinguish it with L2CAP handle */
p_cb->handle = (i + 1) | BTA_JV_RFCOMM_MASK;
p_cb->max_sess = 1;
p_cb->curr_sess = 1;
for (j = 0; j < BTA_JV_MAX_RFC_SR_SESSION; j++) {
p_cb->rfc_hdl[j] = 0;
}
p_cb->rfc_hdl[0] = port_handle;
APPL_TRACE_DEBUG( "bta_jv_alloc_rfc_cb port_handle:%d handle:0x%2x",
port_handle, p_cb->handle);
p_pcb = &bta_jv_cb.port_cb[port_handle - 1];
p_pcb->handle = p_cb->handle;
p_pcb->port_handle = port_handle;
p_pcb->p_pm_cb = NULL;
*pp_pcb = p_pcb;
break;
}
}
if (p_cb == NULL) {
APPL_TRACE_ERROR( "bta_jv_alloc_rfc_cb: port_handle:%d, ctrl block exceeds "
"limit:%d", port_handle, BTA_JV_MAX_RFC_CONN);
}
return p_cb;
}
/*******************************************************************************
**
** Function bta_jv_rfc_port_to_pcb
**
** Description find the port control block associated with the given port handle
**
** Returns
**
*******************************************************************************/
tBTA_JV_PCB *bta_jv_rfc_port_to_pcb(UINT16 port_handle)
{
tBTA_JV_PCB *p_pcb = NULL;
if ((port_handle > 0) && (port_handle <= MAX_RFC_PORTS)
&& bta_jv_cb.port_cb[port_handle - 1].handle) {
p_pcb = &bta_jv_cb.port_cb[port_handle - 1];
}
return p_pcb;
}
/*******************************************************************************
**
** Function bta_jv_rfc_port_to_cb
**
** Description find the RFCOMM control block associated with the given port handle
**
** Returns
**
*******************************************************************************/
tBTA_JV_RFC_CB *bta_jv_rfc_port_to_cb(UINT16 port_handle)
{
tBTA_JV_RFC_CB *p_cb = NULL;
UINT32 handle;
if ((port_handle > 0) && (port_handle <= MAX_RFC_PORTS)
&& bta_jv_cb.port_cb[port_handle - 1].handle) {
handle = bta_jv_cb.port_cb[port_handle - 1].handle;
handle &= BTA_JV_RFC_HDL_MASK;
handle &= ~BTA_JV_RFCOMM_MASK;
if (handle) {
p_cb = &bta_jv_cb.rfc_cb[handle - 1];
}
} else {
APPL_TRACE_WARNING("bta_jv_rfc_port_to_cb(port_handle:0x%x):jv handle:0x%x not"
" FOUND", port_handle, bta_jv_cb.port_cb[port_handle - 1].handle);
}
return p_cb;
}
static tBTA_JV_STATUS bta_jv_free_rfc_cb(tBTA_JV_RFC_CB *p_cb, tBTA_JV_PCB *p_pcb)
{
tBTA_JV_STATUS status = BTA_JV_SUCCESS;
BOOLEAN remove_server = FALSE;
int close_pending = 0;
if (!p_cb || !p_pcb) {
APPL_TRACE_ERROR("bta_jv_free_sr_rfc_cb, p_cb or p_pcb cannot be null");
return BTA_JV_FAILURE;
}
APPL_TRACE_DEBUG("bta_jv_free_sr_rfc_cb: max_sess:%d, curr_sess:%d, p_pcb:%p, user:"
"%p, state:%d, jv handle: 0x%x" , p_cb->max_sess, p_cb->curr_sess, p_pcb,
p_pcb->user_data, p_pcb->state, p_pcb->handle);
if (p_cb->curr_sess <= 0) {
return BTA_JV_SUCCESS;
}
switch (p_pcb->state) {
case BTA_JV_ST_CL_CLOSING:
case BTA_JV_ST_SR_CLOSING:
APPL_TRACE_WARNING("bta_jv_free_sr_rfc_cb: return on closing, port state:%d, "
"scn:%d, p_pcb:%p, user_data:%p", p_pcb->state, p_cb->scn, p_pcb,
p_pcb->user_data);
status = BTA_JV_FAILURE;
return status;
case BTA_JV_ST_CL_OPEN:
case BTA_JV_ST_CL_OPENING:
APPL_TRACE_DEBUG("bta_jv_free_sr_rfc_cb: state: %d, scn:%d,"
" user_data:%p", p_pcb->state, p_cb->scn, p_pcb->user_data);
p_pcb->state = BTA_JV_ST_CL_CLOSING;
break;
case BTA_JV_ST_SR_LISTEN:
p_pcb->state = BTA_JV_ST_SR_CLOSING;
remove_server = TRUE;
APPL_TRACE_DEBUG("bta_jv_free_sr_rfc_cb: state: BTA_JV_ST_SR_LISTEN, scn:%d,"
" user_data:%p", p_cb->scn, p_pcb->user_data);
break;
case BTA_JV_ST_SR_OPEN:
p_pcb->state = BTA_JV_ST_SR_CLOSING;
APPL_TRACE_DEBUG("bta_jv_free_sr_rfc_cb: state: BTA_JV_ST_SR_OPEN, scn:%d,"
" user_data:%p", p_cb->scn, p_pcb->user_data);
break;
default:
APPL_TRACE_WARNING("bta_jv_free_sr_rfc_cb():failed, ignore port state:%d, scn:"
"%d, p_pcb:%p, jv handle: 0x%x, port_handle: %d, user_data:%p",
p_pcb->state, p_cb->scn, p_pcb, p_pcb->handle, p_pcb->port_handle,
p_pcb->user_data);
status = BTA_JV_FAILURE;
break;
}
if (BTA_JV_SUCCESS == status) {
int port_status;
if (!remove_server) {
port_status = RFCOMM_RemoveConnection(p_pcb->port_handle);
} else {
port_status = RFCOMM_RemoveServer(p_pcb->port_handle);
}
if (port_status != PORT_SUCCESS) {
status = BTA_JV_FAILURE;
APPL_TRACE_WARNING("bta_jv_free_rfc_cb(jv handle: 0x%x, state %d)::"
"port_status: %d, port_handle: %d, close_pending: %d:Remove",
p_pcb->handle, p_pcb->state, port_status, p_pcb->port_handle,
close_pending);
}
}
if (!close_pending) {
p_pcb->port_handle = 0;
p_pcb->state = BTA_JV_ST_NONE;
bta_jv_free_set_pm_profile_cb(p_pcb->handle);
//Initialize congestion flags
p_pcb->cong = FALSE;
p_pcb->user_data = 0;
int si = BTA_JV_RFC_HDL_TO_SIDX(p_pcb->handle);
if (0 <= si && si < BTA_JV_MAX_RFC_SR_SESSION) {
p_cb->rfc_hdl[si] = 0;
}
p_pcb->handle = 0;
p_cb->curr_sess--;
if (p_cb->curr_sess == 0) {
p_cb->scn = 0;
bta_jv_free_sec_id(&p_cb->sec_id);
p_cb->p_cback = NULL;
p_cb->handle = 0;
p_cb->curr_sess = -1;
}
if (remove_server) {
bta_jv_free_sec_id(&p_cb->sec_id);
}
}
return status;
}
/*******************************************************************************
**
** Function bta_jv_free_l2c_cb
**
** Description free the given L2CAP control block
**
** Returns
**
*******************************************************************************/
tBTA_JV_STATUS bta_jv_free_l2c_cb(tBTA_JV_L2C_CB *p_cb)
{
tBTA_JV_STATUS status = BTA_JV_SUCCESS;
if (BTA_JV_ST_NONE != p_cb->state) {
bta_jv_free_set_pm_profile_cb((UINT32)p_cb->handle);
if (GAP_ConnClose(p_cb->handle) != BT_PASS) {
status = BTA_JV_FAILURE;
}
}
p_cb->psm = 0;
p_cb->state = BTA_JV_ST_NONE;
bta_jv_free_sec_id(&p_cb->sec_id);
p_cb->p_cback = NULL;
return status;
}
/*******************************************************************************
**
**
** Function bta_jv_clear_pm_cb
**
** Description clears jv pm control block and optionally calls bta_sys_conn_close()
** In general close_conn should be set to TRUE to remove registering with
** dm pm!
**
** WARNING: Make sure to clear pointer form port or l2c to this control block too!
**
*******************************************************************************/
static void bta_jv_clear_pm_cb(tBTA_JV_PM_CB *p_pm_cb, BOOLEAN close_conn)
{
/* needs to be called if registered with bta pm, otherwise we may run out of dm pm slots! */
if (close_conn) {
bta_sys_conn_close(BTA_ID_JV, p_pm_cb->app_id, p_pm_cb->peer_bd_addr);
}
p_pm_cb->state = BTA_JV_PM_FREE_ST;
p_pm_cb->app_id = BTA_JV_PM_ALL;
p_pm_cb->handle = BTA_JV_PM_HANDLE_CLEAR;
bdcpy(p_pm_cb->peer_bd_addr, bd_addr_null);
}
/*******************************************************************************
**
** Function bta_jv_free_set_pm_profile_cb
**
** Description free pm profile control block
**
** Returns BTA_JV_SUCCESS if cb has been freed correctly,
** BTA_JV_FAILURE in case of no profile has been registered or already freed
**
*******************************************************************************/
static tBTA_JV_STATUS bta_jv_free_set_pm_profile_cb(UINT32 jv_handle)
{
tBTA_JV_STATUS status = BTA_JV_FAILURE;
tBTA_JV_PM_CB **p_cb;
int i, j, bd_counter = 0, appid_counter = 0;
for (i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
p_cb = NULL;
if ((bta_jv_cb.pm_cb[i].state != BTA_JV_PM_FREE_ST) &&
(jv_handle == bta_jv_cb.pm_cb[i].handle)) {
for (j = 0; j < BTA_JV_PM_MAX_NUM; j++) {
if (bdcmp(bta_jv_cb.pm_cb[j].peer_bd_addr, bta_jv_cb.pm_cb[i].peer_bd_addr) == 0) {
bd_counter++;
}
if (bta_jv_cb.pm_cb[j].app_id == bta_jv_cb.pm_cb[i].app_id) {
appid_counter++;
}
}
APPL_TRACE_API("%s(jv_handle: 0x%2x), idx: %d, "
"app_id: 0x%x", __func__, jv_handle, i, bta_jv_cb.pm_cb[i].app_id);
APPL_TRACE_API("%s, bd_counter = %d, "
"appid_counter = %d", __func__, bd_counter, appid_counter);
if (bd_counter > 1) {
bta_jv_pm_conn_idle(&bta_jv_cb.pm_cb[i]);
}
if (bd_counter <= 1 || (appid_counter <= 1)) {
bta_jv_clear_pm_cb(&bta_jv_cb.pm_cb[i], TRUE);
} else {
bta_jv_clear_pm_cb(&bta_jv_cb.pm_cb[i], FALSE);
}
if (BTA_JV_RFCOMM_MASK & jv_handle) {
UINT32 hi = ((jv_handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
UINT32 si = BTA_JV_RFC_HDL_TO_SIDX(jv_handle);
if (hi < BTA_JV_MAX_RFC_CONN && bta_jv_cb.rfc_cb[hi].p_cback && si
< BTA_JV_MAX_RFC_SR_SESSION && bta_jv_cb.rfc_cb[hi].rfc_hdl[si]) {
tBTA_JV_PCB *p_pcb = bta_jv_rfc_port_to_pcb(bta_jv_cb.rfc_cb[hi].rfc_hdl[si]);
if (p_pcb) {
if (NULL == p_pcb->p_pm_cb)
APPL_TRACE_WARNING("%s(jv_handle:"
" 0x%x):port_handle: 0x%x, p_pm_cb: %d: no link to "
"pm_cb?", __func__, jv_handle, p_pcb->port_handle, i);
p_cb = &p_pcb->p_pm_cb;
}
}
} else {
if (jv_handle < BTA_JV_MAX_L2C_CONN) {
tBTA_JV_L2C_CB *p_l2c_cb = &bta_jv_cb.l2c_cb[jv_handle];
if (NULL == p_l2c_cb->p_pm_cb)
APPL_TRACE_WARNING("%s(jv_handle: "
"0x%x): p_pm_cb: %d: no link to pm_cb?", __func__, jv_handle, i);
p_cb = &p_l2c_cb->p_pm_cb;
}
}
if (p_cb) {
*p_cb = NULL;
status = BTA_JV_SUCCESS;
}
}
}
return status;
}
/*******************************************************************************
**
** Function bta_jv_alloc_set_pm_profile_cb
**
** Description set PM profile control block
**
** Returns pointer to allocated cb or NULL in case of failure
**
*******************************************************************************/
static tBTA_JV_PM_CB *bta_jv_alloc_set_pm_profile_cb(UINT32 jv_handle, tBTA_JV_PM_ID app_id)
{
BOOLEAN bRfcHandle = (jv_handle & BTA_JV_RFCOMM_MASK) != 0;
BD_ADDR peer_bd_addr;
int i, j;
tBTA_JV_PM_CB **pp_cb;
for (i = 0; i < BTA_JV_PM_MAX_NUM; i++) {
pp_cb = NULL;
if (bta_jv_cb.pm_cb[i].state == BTA_JV_PM_FREE_ST) {
/* rfc handle bd addr retrieval requires core stack handle */
if (bRfcHandle) {
// UINT32 hi = ((jv_handle & BTA_JV_RFC_HDL_MASK) & ~BTA_JV_RFCOMM_MASK) - 1;
// UINT32 si = BTA_JV_RFC_HDL_TO_SIDX(jv_handle);
for (j = 0; j < BTA_JV_MAX_RFC_CONN; j++) {
if (jv_handle == bta_jv_cb.port_cb[j].handle) {
pp_cb = &bta_jv_cb.port_cb[j].p_pm_cb;
if (PORT_SUCCESS != PORT_CheckConnection(
bta_jv_cb.port_cb[j].port_handle, peer_bd_addr, NULL)) {
i = BTA_JV_PM_MAX_NUM;
}
break;
}
}
} else {
/* use jv handle for l2cap bd address retrieval */
for (j = 0; j < BTA_JV_MAX_L2C_CONN; j++) {
if (jv_handle == bta_jv_cb.l2c_cb[j].handle) {
pp_cb = &bta_jv_cb.l2c_cb[j].p_pm_cb;
UINT8 *p_bd_addr = GAP_ConnGetRemoteAddr((UINT16)jv_handle);
if (NULL != p_bd_addr) {
bdcpy(peer_bd_addr, p_bd_addr);
} else {
i = BTA_JV_PM_MAX_NUM;
}
break;
}
}
}
APPL_TRACE_API("bta_jv_alloc_set_pm_profile_cb(handle 0x%2x, app_id %d): "
"idx: %d, (BTA_JV_PM_MAX_NUM: %d), pp_cb: %p", jv_handle, app_id,
i, BTA_JV_PM_MAX_NUM, (void *)pp_cb);
break;
}
}
if ((i != BTA_JV_PM_MAX_NUM) && (NULL != pp_cb)) {
*pp_cb = &bta_jv_cb.pm_cb[i];
bta_jv_cb.pm_cb[i].handle = jv_handle;
bta_jv_cb.pm_cb[i].app_id = app_id;
bdcpy(bta_jv_cb.pm_cb[i].peer_bd_addr, peer_bd_addr);
bta_jv_cb.pm_cb[i].state = BTA_JV_PM_IDLE_ST;
return &bta_jv_cb.pm_cb[i];
}
APPL_TRACE_WARNING("bta_jv_alloc_set_pm_profile_cb(jv_handle: 0x%x, app_id: %d) "
"return NULL", jv_handle, app_id);
return (tBTA_JV_PM_CB *)NULL;
}
/*******************************************************************************
**
** Function bta_jv_check_psm
**
** Description for now use only the legal PSM per JSR82 spec
**
** Returns TRUE, if allowed
**
*******************************************************************************/
BOOLEAN bta_jv_check_psm(UINT16 psm)
{
BOOLEAN ret = FALSE;
if (L2C_IS_VALID_PSM(psm)) {
if (psm < 0x1001) {
/* see if this is defined by spec */
switch (psm) {
case SDP_PSM: /* 1 */
case BT_PSM_RFCOMM: /* 3 */
/* do not allow java app to use these 2 PSMs */
break;
case TCS_PSM_INTERCOM: /* 5 */
case TCS_PSM_CORDLESS: /* 7 */
if ( FALSE == bta_sys_is_register(BTA_ID_CT) &&
FALSE == bta_sys_is_register(BTA_ID_CG) ) {
ret = TRUE;
}
break;
case BT_PSM_BNEP: /* F */
if (FALSE == bta_sys_is_register(BTA_ID_PAN)) {
ret = TRUE;
}
break;
case HID_PSM_CONTROL: /* 0x11 */
case HID_PSM_INTERRUPT: /* 0x13 */
//FIX: allow HID Device and HID Host to coexist
if ( FALSE == bta_sys_is_register(BTA_ID_HD) ||
FALSE == bta_sys_is_register(BTA_ID_HH) ) {
ret = TRUE;
}
break;
case AVCT_PSM: /* 0x17 */
case AVDT_PSM: /* 0x19 */
if ((FALSE == bta_sys_is_register(BTA_ID_AV)) &&
(FALSE == bta_sys_is_register(BTA_ID_AVK))) {
ret = TRUE;
}
break;
default:
ret = TRUE;
break;
}
} else {
ret = TRUE;
}
}
return ret;
}
/*******************************************************************************
**
** Function bta_jv_enable
**
** Description Initialises the JAVA I/F
**
** Returns void
**
*******************************************************************************/
void bta_jv_enable(tBTA_JV_MSG *p_data)
{
tBTA_JV_STATUS status = BTA_JV_SUCCESS;
bta_jv_cb.p_dm_cback = p_data->enable.p_cback;
bta_jv_cb.p_dm_cback(BTA_JV_ENABLE_EVT, (tBTA_JV *)&status, 0);
memset(bta_jv_cb.free_psm_list, 0, sizeof(bta_jv_cb.free_psm_list));
}
/*******************************************************************************
**
** Function bta_jv_disable
**
** Description Disables the BT device manager
** free the resources used by java
**
** Returns void
**
*******************************************************************************/
void bta_jv_disable (tBTA_JV_MSG *p_data)
{
UNUSED(p_data);
APPL_TRACE_ERROR("%s", __func__);
}
/**
* We keep a list of PSM's that have been freed from JAVA, for reuse.
* This function will return a free PSM, and delete it from the free
* list.
* If no free PSMs exist, 0 will be returned.
*/
static UINT16 bta_jv_get_free_psm()
{
const int cnt = sizeof(bta_jv_cb.free_psm_list) / sizeof(bta_jv_cb.free_psm_list[0]);
for (int i = 0; i < cnt; i++) {
UINT16 psm = bta_jv_cb.free_psm_list[i];
if (psm != 0) {
APPL_TRACE_DEBUG("%s(): Reusing PSM: 0x%04d", __func__, psm)
bta_jv_cb.free_psm_list[i] = 0;
return psm;
}
}
return 0;
}
static void bta_jv_set_free_psm(UINT16 psm)
{
int free_index = -1;
const int cnt = sizeof(bta_jv_cb.free_psm_list) / sizeof(bta_jv_cb.free_psm_list[0]);
for (int i = 0; i < cnt; i++) {
if (bta_jv_cb.free_psm_list[i] == 0) {
free_index = i;
} else if (psm == bta_jv_cb.free_psm_list[i]) {
return; // PSM already freed?
}
}
if (free_index != -1) {
bta_jv_cb.free_psm_list[free_index] = psm;
APPL_TRACE_DEBUG("%s(): Recycling PSM: 0x%04d", __func__, psm)
} else {
APPL_TRACE_ERROR("%s unable to free psm 0x%x no more free slots", __func__, psm);
}
}
/*******************************************************************************
**
** Function bta_jv_get_channel_id
**
** Description Obtain a free SCN (Server Channel Number)
** (RFCOMM channel or L2CAP PSM)
**
** Returns void
**
*******************************************************************************/
void bta_jv_get_channel_id(tBTA_JV_MSG *p_data)
{
UINT16 psm = 0;
switch (p_data->alloc_channel.type) {
case BTA_JV_CONN_TYPE_RFCOMM: {
INT32 channel = p_data->alloc_channel.channel;
UINT8 scn = 0;
if (channel > 0) {
if (BTM_TryAllocateSCN(channel) == FALSE) {
APPL_TRACE_ERROR("rfc channel:%d already in use or invalid", channel);
channel = 0;
}
} else if ((channel = BTM_AllocateSCN()) == 0) {
APPL_TRACE_ERROR("run out of rfc channels");
channel = 0;
}
if (channel != 0) {
bta_jv_cb.scn[channel - 1] = TRUE;
scn = (UINT8) channel;
}
if (bta_jv_cb.p_dm_cback)
bta_jv_cb.p_dm_cback(BTA_JV_GET_SCN_EVT, (tBTA_JV *)&scn,
p_data->alloc_channel.user_data);
return;
}
case BTA_JV_CONN_TYPE_L2CAP:
psm = bta_jv_get_free_psm();
if (psm == 0) {
psm = L2CA_AllocatePSM();
APPL_TRACE_DEBUG("%s() returned PSM: 0x%04x", __func__, psm);
}
break;
case BTA_JV_CONN_TYPE_L2CAP_LE:
break;
default:
break;
}
if (bta_jv_cb.p_dm_cback) {
bta_jv_cb.p_dm_cback(BTA_JV_GET_PSM_EVT, (tBTA_JV *)&psm, p_data->alloc_channel.user_data);
}
}
/*******************************************************************************
**
** Function bta_jv_free_scn
**
** Description free a SCN
**
** Returns void
**
*******************************************************************************/
void bta_jv_free_scn(tBTA_JV_MSG *p_data)
{
UINT16 scn = p_data->free_channel.scn;
switch (p_data->free_channel.type) {
case BTA_JV_CONN_TYPE_RFCOMM: {
if (scn > 0 && scn <= BTA_JV_MAX_SCN && bta_jv_cb.scn[scn - 1]) {
/* this scn is used by JV */
bta_jv_cb.scn[scn - 1] = FALSE;
BTM_FreeSCN(scn);
}
break;
}
case BTA_JV_CONN_TYPE_L2CAP:
bta_jv_set_free_psm(scn);
break;
case BTA_JV_CONN_TYPE_L2CAP_LE:
// TODO: Not yet implemented...
break;
default:
break;
}
}
static inline tBT_UUID shorten_sdp_uuid(const tBT_UUID *u)
{
static uint8_t bt_base_uuid[] =
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB };
logu("in, uuid:", u->uu.uuid128);
APPL_TRACE_DEBUG("uuid len:%d", u->len);
if (u->len == 16) {
if (memcmp(&u->uu.uuid128[4], &bt_base_uuid[4], 12) == 0) {
tBT_UUID su;
memset(&su, 0, sizeof(su));
if (u->uu.uuid128[0] == 0 && u->uu.uuid128[1] == 0) {
su.len = 2;
uint16_t u16;
memcpy(&u16, &u->uu.uuid128[2], sizeof(u16));
su.uu.uuid16 = ntohs(u16);
APPL_TRACE_DEBUG("shorten to 16 bits uuid: %x", su.uu.uuid16);
} else {
su.len = 4;
uint32_t u32;
memcpy(&u32, &u->uu.uuid128[0], sizeof(u32));
su.uu.uuid32 = ntohl(u32);
APPL_TRACE_DEBUG("shorten to 32 bits uuid: %x", su.uu.uuid32);
}
return su;
}
}
APPL_TRACE_DEBUG("cannot shorten none-reserved 128 bits uuid");
return *u;
}
/*******************************************************************************
**
** Function bta_jv_start_discovery_cback
**
** Description Callback for Start Discovery
**
** Returns void
**
*******************************************************************************/
static void bta_jv_start_discovery_cback(UINT16 result, void *user_data)
{
tBTA_JV_STATUS status;
// UINT8 old_sdp_act = bta_jv_cb.sdp_active;
APPL_TRACE_DEBUG("bta_jv_start_discovery_cback res: 0x%x", result);
bta_jv_cb.sdp_active = BTA_JV_SDP_ACT_NONE;
if (bta_jv_cb.p_dm_cback) {
tBTA_JV_DISCOVERY_COMP dcomp;
dcomp.scn_num = 0;
status = BTA_JV_FAILURE;
if (result == SDP_SUCCESS || result == SDP_DB_FULL) {
tSDP_DISC_REC *p_sdp_rec = NULL;
tSDP_PROTOCOL_ELEM pe;
logu("bta_jv_cb.uuid", bta_jv_cb.uuid.uu.uuid128);
tBT_UUID su = shorten_sdp_uuid(&bta_jv_cb.uuid);
logu("shorten uuid:", su.uu.uuid128);
do{
p_sdp_rec = SDP_FindServiceUUIDInDb(p_bta_jv_cfg->p_sdp_db, &su, p_sdp_rec);
APPL_TRACE_DEBUG("p_sdp_rec:%p", p_sdp_rec);
if (p_sdp_rec && SDP_FindProtocolListElemInRec(p_sdp_rec, UUID_PROTOCOL_RFCOMM, &pe)){
dcomp.scn[dcomp.scn_num++] = (UINT8) pe.params[0];
status = BTA_JV_SUCCESS;
}
} while (p_sdp_rec);
}
dcomp.status = status;
bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, (tBTA_JV *)&dcomp, user_data);
}
}
/*******************************************************************************
**
** Function bta_jv_start_discovery
**
** Description Discovers services on a remote device
**
** Returns void
**
*******************************************************************************/
void bta_jv_start_discovery(tBTA_JV_MSG *p_data)
{
tBTA_JV_STATUS status = BTA_JV_FAILURE;
APPL_TRACE_DEBUG("bta_jv_start_discovery in, sdp_active:%d", bta_jv_cb.sdp_active);
if (bta_jv_cb.sdp_active != BTA_JV_SDP_ACT_NONE) {
/* SDP is still in progress */
status = BTA_JV_BUSY;
if (bta_jv_cb.p_dm_cback) {
bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, (tBTA_JV *)&status, p_data->start_discovery.user_data);
}
return;
}
/* init the database/set up the filter */
APPL_TRACE_DEBUG("call SDP_InitDiscoveryDb, p_data->start_discovery.num_uuid:%d",
p_data->start_discovery.num_uuid);
SDP_InitDiscoveryDb (p_bta_jv_cfg->p_sdp_db, p_bta_jv_cfg->sdp_db_size,
p_data->start_discovery.num_uuid, p_data->start_discovery.uuid_list, 0, NULL);
/* tell SDP to keep the raw data */
p_bta_jv_cfg->p_sdp_db->raw_data = p_bta_jv_cfg->p_sdp_raw_data;
p_bta_jv_cfg->p_sdp_db->raw_size = p_bta_jv_cfg->sdp_raw_size;
bta_jv_cb.p_sel_raw_data = 0;
bta_jv_cb.uuid = p_data->start_discovery.uuid_list[0];
bta_jv_cb.sdp_active = BTA_JV_SDP_ACT_YES;
if (!SDP_ServiceSearchAttributeRequest2(p_data->start_discovery.bd_addr,
p_bta_jv_cfg->p_sdp_db,
bta_jv_start_discovery_cback, p_data->start_discovery.user_data)) {
bta_jv_cb.sdp_active = BTA_JV_SDP_ACT_NONE;
/* failed to start SDP. report the failure right away */
if (bta_jv_cb.p_dm_cback) {
bta_jv_cb.p_dm_cback(BTA_JV_DISCOVERY_COMP_EVT, (tBTA_JV *)&status, p_data->start_discovery.user_data);
}
}
/*
else report the result when the cback is called
*/
}
/**
* @brief Adds a protocol list and service name (if provided) to an SDP record given by
* sdp_handle, and marks it as browseable. This is a shortcut for defining a
* set of protocols that includes L2CAP, RFCOMM, and optionally OBEX.
*
* @param[in] sdp_handle: SDP handle
* @param[in] name: service name
* @param[in] channel: channel
* @param[in] with_obex: if TRUE, then an additional OBEX protocol UUID will be included
* at the end of the protocol list.
* @return
* - TRUE : success
* - other : failed
*/
static bool create_base_record(const uint32_t sdp_handle, const char *name, const uint16_t channel, const bool with_obex){
APPL_TRACE_DEBUG("create_base_record: scn: %d, name: %s, with_obex: %d",
channel, name, with_obex);
// Setup the protocol list and add it.
tSDP_PROTOCOL_ELEM proto_list[SDP_MAX_LIST_ELEMS];
int num_proto_elements = with_obex ? 3 : 2;
memset(proto_list, 0, num_proto_elements * sizeof(tSDP_PROTOCOL_ELEM));
proto_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
proto_list[0].num_params = 0;
proto_list[1].protocol_uuid = UUID_PROTOCOL_RFCOMM;
proto_list[1].num_params = 1;
proto_list[1].params[0] = channel;
if (with_obex == TRUE) {
proto_list[2].protocol_uuid = UUID_PROTOCOL_OBEX;
proto_list[2].num_params = 0;
}
const char *stage = "protocol_list";
if (!SDP_AddProtocolList(sdp_handle, num_proto_elements, proto_list)){
APPL_TRACE_ERROR("create_base_record: failed to create base service "
"record, stage: %s, scn: %d, name: %s, with_obex: %d",
stage, channel, name, with_obex);
return FALSE;
}
stage = "profile_descriptor_list";
if (!SDP_AddProfileDescriptorList(sdp_handle, UUID_SERVCLASS_SERIAL_PORT, SPP_VERSION)){
APPL_TRACE_ERROR("create_base_record: failed to create base service "
"record, stage: %s, scn: %d, name: %s, with_obex: %d",
stage, channel, name, with_obex);
return FALSE;
}
// Add the name to the SDP record.
if (name[0] != '\0') {
stage = "service_name";
if (!SDP_AddAttribute(sdp_handle, ATTR_ID_SERVICE_NAME,
TEXT_STR_DESC_TYPE, (uint32_t)(strlen(name) + 1),
(uint8_t *)name)){
APPL_TRACE_ERROR("create_base_record: failed to create base service "
"record, stage: %s, scn: %d, name: %s, with_obex: %d",
stage, channel, name, with_obex);
return FALSE;
}
}
// Mark the service as browseable.
uint16_t list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
stage = "browseable";
if (!SDP_AddUuidSequence(sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &list)){
APPL_TRACE_ERROR("create_base_record: failed to create base service "
"record, stage: %s, scn: %d, name: %s, with_obex: %d",
stage, channel, name, with_obex);
return FALSE;
}
APPL_TRACE_DEBUG("create_base_record: successfully created base service "
"record, handle: 0x%08x, scn: %d, name: %s, with_obex: %d",
sdp_handle, channel, name, with_obex);
return TRUE;
}
static int add_spp_sdp(const char *name, const int channel) {
APPL_TRACE_DEBUG("add_spp_sdp: scn %d, service_name %s", channel, name);