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
|
/******************************************************************************
*
* Copyright (c) 2016 The Android Open Source Project
* Copyright 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.
*
******************************************************************************/
#include <cstdint>
#include <cstdio>
#include "bta/hf_client/bta_hf_client_int.h"
#include "bta/include/utl.h"
#include "osi/include/allocator.h"
#include "osi/include/osi.h" // UNUSED_ATTR
#include "stack/include/bt_hdr.h"
#include "stack/include/btm_api.h"
#include "types/raw_address.h"
#include <base/logging.h>
static const char* bta_hf_client_evt_str(uint16_t event);
static const char* bta_hf_client_state_str(uint8_t state);
void bta_hf_client_cb_init(tBTA_HF_CLIENT_CB* client_cb, uint16_t handle);
/* state machine states */
enum {
BTA_HF_CLIENT_INIT_ST,
BTA_HF_CLIENT_OPENING_ST,
BTA_HF_CLIENT_OPEN_ST,
BTA_HF_CLIENT_CLOSING_ST
};
/* state machine action enumeration list */
enum {
BTA_HF_CLIENT_RFC_DO_CLOSE,
BTA_HF_CLIENT_START_CLOSE,
BTA_HF_CLIENT_START_OPEN,
BTA_HF_CLIENT_RFC_ACP_OPEN,
BTA_HF_CLIENT_SCO_LISTEN,
BTA_HF_CLIENT_SCO_CONN_OPEN,
BTA_HF_CLIENT_SCO_CONN_CLOSE,
BTA_HF_CLIENT_SCO_OPEN,
BTA_HF_CLIENT_SCO_CLOSE,
BTA_HF_CLIENT_FREE_DB,
BTA_HF_CLIENT_OPEN_FAIL,
BTA_HF_CLIENT_RFC_OPEN,
BTA_HF_CLIENT_RFC_FAIL,
BTA_HF_CLIENT_DISC_INT_RES,
BTA_HF_CLIENT_RFC_DO_OPEN,
BTA_HF_CLIENT_DISC_FAIL,
BTA_HF_CLIENT_RFC_CLOSE,
BTA_HF_CLIENT_RFC_DATA,
BTA_HF_CLIENT_DISC_ACP_RES,
BTA_HF_CLIENT_SVC_CONN_OPEN,
BTA_HF_CLIENT_SEND_AT_CMD,
BTA_HF_CLIENT_NUM_ACTIONS,
};
#define BTA_HF_CLIENT_IGNORE BTA_HF_CLIENT_NUM_ACTIONS
/* type for action functions */
typedef void (*tBTA_HF_CLIENT_ACTION)(tBTA_HF_CLIENT_DATA* p_data);
/* action functions table, indexed with action enum */
const tBTA_HF_CLIENT_ACTION bta_hf_client_action[] = {
/* BTA_HF_CLIENT_RFC_DO_CLOSE */ bta_hf_client_rfc_do_close,
/* BTA_HF_CLIENT_START_CLOSE */ bta_hf_client_start_close,
/* BTA_HF_CLIENT_START_OPEN */ bta_hf_client_start_open,
/* BTA_HF_CLIENT_RFC_ACP_OPEN */ bta_hf_client_rfc_acp_open,
/* BTA_HF_CLIENT_SCO_LISTEN */ NULL,
/* BTA_HF_CLIENT_SCO_CONN_OPEN */ bta_hf_client_sco_conn_open,
/* BTA_HF_CLIENT_SCO_CONN_CLOSE*/ bta_hf_client_sco_conn_close,
/* BTA_HF_CLIENT_SCO_OPEN */ bta_hf_client_sco_open,
/* BTA_HF_CLIENT_SCO_CLOSE */ bta_hf_client_sco_close,
/* BTA_HF_CLIENT_FREE_DB */ bta_hf_client_free_db,
/* BTA_HF_CLIENT_OPEN_FAIL */ bta_hf_client_open_fail,
/* BTA_HF_CLIENT_RFC_OPEN */ bta_hf_client_rfc_open,
/* BTA_HF_CLIENT_RFC_FAIL */ bta_hf_client_rfc_fail,
/* BTA_HF_CLIENT_DISC_INT_RES */ bta_hf_client_disc_int_res,
/* BTA_HF_CLIENT_RFC_DO_OPEN */ bta_hf_client_rfc_do_open,
/* BTA_HF_CLIENT_DISC_FAIL */ bta_hf_client_disc_fail,
/* BTA_HF_CLIENT_RFC_CLOSE */ bta_hf_client_rfc_close,
/* BTA_HF_CLIENT_RFC_DATA */ bta_hf_client_rfc_data,
/* BTA_HF_CLIENT_DISC_ACP_RES */ bta_hf_client_disc_acp_res,
/* BTA_HF_CLIENT_SVC_CONN_OPEN */ bta_hf_client_svc_conn_open,
/* BTA_HF_CLIENT_SEND_AT_CMD */ bta_hf_client_send_at_cmd,
};
/* state table information */
#define BTA_HF_CLIENT_ACTIONS 2 /* number of actions */
#define BTA_HF_CLIENT_NEXT_STATE 2 /* position of next state */
#define BTA_HF_CLIENT_NUM_COLS 3 /* number of columns in state tables */
/* state table for init state */
const uint8_t bta_hf_client_st_init[][BTA_HF_CLIENT_NUM_COLS] = {
/* Event Action 1 Action 2
Next state */
/* API_OPEN_EVT */ {BTA_HF_CLIENT_START_OPEN, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* API_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* RFC_OPEN_EVT */ {BTA_HF_CLIENT_RFC_ACP_OPEN, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
};
/* state table for opening state */
const uint8_t bta_hf_client_st_opening[][BTA_HF_CLIENT_NUM_COLS] = {
/* Event Action 1 Action 2
Next state */
/* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* API_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_DO_CLOSE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* RFC_OPEN_EVT */ {BTA_HF_CLIENT_RFC_OPEN, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_FAIL, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_DISC_INT_RES, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* DISC_OK_EVT */ {BTA_HF_CLIENT_RFC_DO_OPEN, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* DISC_FAIL_EVT */ {BTA_HF_CLIENT_DISC_FAIL, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
/* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPENING_ST},
};
/* state table for open state */
const uint8_t bta_hf_client_st_open[][BTA_HF_CLIENT_NUM_COLS] = {
/* Event Action 1 Action 2
Next state */
/* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* API_CLOSE_EVT */ {BTA_HF_CLIENT_START_CLOSE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_SCO_OPEN, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_SCO_CLOSE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* RFC_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_CLOSE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* RFC_DATA_EVT */ {BTA_HF_CLIENT_RFC_DATA, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_DISC_ACP_RES, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* SCO_OPEN_EVT */ {BTA_HF_CLIENT_SCO_CONN_OPEN, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_SCO_CONN_CLOSE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
/* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_SEND_AT_CMD, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_OPEN_ST},
};
/* state table for closing state */
const uint8_t bta_hf_client_st_closing[][BTA_HF_CLIENT_NUM_COLS] = {
/* Event Action 1 Action 2
Next state */
/* API_OPEN_EVT */ {BTA_HF_CLIENT_OPEN_FAIL, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* API_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* API_AUDIO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* API_AUDIO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* RFC_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* RFC_CLOSE_EVT */ {BTA_HF_CLIENT_RFC_CLOSE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* RFC_SRV_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* RFC_DATA_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* DISC_ACP_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* DISC_INT_RES_EVT */ {BTA_HF_CLIENT_FREE_DB, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_INIT_ST},
/* DISC_OK_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* DISC_FAIL_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* SCO_OPEN_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* SCO_CLOSE_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
/* SEND_AT_CMD_EVT */ {BTA_HF_CLIENT_IGNORE, BTA_HF_CLIENT_IGNORE,
BTA_HF_CLIENT_CLOSING_ST},
};
/* type for state table */
typedef const uint8_t (*tBTA_HF_CLIENT_ST_TBL)[BTA_HF_CLIENT_NUM_COLS];
/* state table */
const tBTA_HF_CLIENT_ST_TBL bta_hf_client_st_tbl[] = {
bta_hf_client_st_init, bta_hf_client_st_opening, bta_hf_client_st_open,
bta_hf_client_st_closing};
/* HF Client control block */
tBTA_HF_CLIENT_CB_ARR bta_hf_client_cb_arr;
/* Event handler for the state machine */
static const tBTA_SYS_REG bta_hf_client_reg = {bta_hf_client_hdl_event,
BTA_HfClientDisable};
/*******************************************************************************
*
* Function bta_hf_client_cb_arr_init
*
* Description Initialize entire control block array set
*
*
* Returns void
*
******************************************************************************/
void bta_hf_client_cb_arr_init() {
memset(&bta_hf_client_cb_arr, 0, sizeof(tBTA_HF_CLIENT_CB_ARR));
// reset the handles and make the CBs non-allocated
for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
// Allocate the handles in increasing order of indices
bta_hf_client_cb_init(&(bta_hf_client_cb_arr.cb[i]), i);
bta_hf_client_cb_arr.cb[i].handle = BTA_HF_CLIENT_CB_FIRST_HANDLE + i;
}
}
/*******************************************************************************
*
* Function bta_hf_client_cb_init
*
* Description Initialize an HF_Client service control block. Assign the
* handle to cb->handle.
*
*
*
* Returns void
*
******************************************************************************/
void bta_hf_client_cb_init(tBTA_HF_CLIENT_CB* client_cb, uint16_t handle) {
APPL_TRACE_DEBUG("%s", __func__);
// Free any memory we need to explicity release
alarm_free(client_cb->collision_timer);
// release unique pointers
client_cb->enabled_hf_indicators.clear();
client_cb->peer_hf_indicators.clear();
// Memset the rest of the block
// memset(client_cb, 0, sizeof(tBTA_HF_CLIENT_CB));
*client_cb = {};
// Re allocate any variables required
client_cb->collision_timer = alarm_new("bta_hf_client.scb_collision_timer");
client_cb->handle = handle;
client_cb->sco_idx = BTM_INVALID_SCO_INDEX;
}
/*******************************************************************************
*
* Function bta_hf_client_resume_open
*
* Description Resume opening process.
*
*
* Returns void
*
******************************************************************************/
void bta_hf_client_resume_open(tBTA_HF_CLIENT_CB* client_cb) {
APPL_TRACE_DEBUG("%s", __func__);
/* resume opening process. */
if (client_cb->state == BTA_HF_CLIENT_INIT_ST) {
client_cb->state = BTA_HF_CLIENT_OPENING_ST;
tBTA_HF_CLIENT_DATA msg;
msg.hdr.layer_specific = client_cb->handle;
msg.api_open.bd_addr = client_cb->peer_addr;
bta_hf_client_start_open(&msg);
}
}
/*******************************************************************************
*
* Function bta_hf_client_collision_timer_cback
*
* Description HF Client connection collision timer callback
*
*
* Returns void
*
******************************************************************************/
static void bta_hf_client_collision_timer_cback(void* data) {
APPL_TRACE_DEBUG("%s", __func__);
tBTA_HF_CLIENT_CB* client_cb = (tBTA_HF_CLIENT_CB*)data;
/* If the peer haven't opened connection, restart opening process */
bta_hf_client_resume_open(client_cb);
}
/*******************************************************************************
*
* Function bta_hf_client_collision_cback
*
* Description Get notified about collision.
*
*
* Returns void
*
******************************************************************************/
void bta_hf_client_collision_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status,
uint8_t id, UNUSED_ATTR uint8_t app_id,
const RawAddress& peer_addr) {
tBTA_HF_CLIENT_CB* client_cb = bta_hf_client_find_cb_by_bda(peer_addr);
if (client_cb != NULL && client_cb->state == BTA_HF_CLIENT_OPENING_ST) {
if (id == BTA_ID_SYS) /* ACL collision */
{
APPL_TRACE_WARNING("HF Client found collision (ACL) ...");
} else if (id == BTA_ID_HS) /* RFCOMM collision */
{
APPL_TRACE_WARNING("HF Client found collision (RFCOMM) ...");
} else {
APPL_TRACE_WARNING("HF Client found collision (\?\?\?) ...");
}
client_cb->state = BTA_HF_CLIENT_INIT_ST;
/* Cancel SDP if it had been started. */
if (client_cb->p_disc_db) {
(void)SDP_CancelServiceSearch(client_cb->p_disc_db);
osi_free_and_reset((void**)&client_cb->p_disc_db);
}
/* reopen registered server */
/* Collision may be detected before or after we close servers. */
bta_hf_client_start_server();
/* Start timer to handle connection opening restart */
alarm_set_on_mloop(client_cb->collision_timer,
BTA_HF_CLIENT_COLLISION_TIMER_MS,
bta_hf_client_collision_timer_cback, (void*)client_cb);
}
}
/*******************************************************************************
*
* Function bta_hf_client_api_enable
*
* Description Handle an API enable event.
*
*
* Returns void
*
******************************************************************************/
tBTA_STATUS bta_hf_client_api_enable(tBTA_HF_CLIENT_CBACK* p_cback,
tBTA_HF_CLIENT_FEAT features,
const char* p_service_name) {
/* If already registered then return error */
if (bta_sys_is_register(BTA_ID_HS)) {
APPL_TRACE_ERROR("BTA HF Client is already enabled, ignoring ...");
return BTA_FAILURE;
}
/* register with BTA system manager */
bta_sys_register(BTA_ID_HS, &bta_hf_client_reg);
/* reset the control blocks */
bta_hf_client_cb_arr_init();
bta_hf_client_cb_arr.p_cback = p_cback;
bta_hf_client_cb_arr.features = features;
/* create SDP records */
bta_hf_client_create_record(&bta_hf_client_cb_arr, p_service_name);
/* set same setting as AG does */
BTM_WriteVoiceSettings(AG_VOICE_SETTINGS);
bta_sys_collision_register(BTA_ID_HS, bta_hf_client_collision_cback);
/* Set the Audio service class bit */
tBTA_UTL_COD cod;
cod.service = BTM_COD_SERVICE_AUDIO;
utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
/* start RFCOMM server */
bta_hf_client_start_server();
return BTA_SUCCESS;
}
/*******************************************************************************
*
* Function bta_hf_client_find_cb_by_handle
*
* Description Finds the control block by handle provided
*
* handle: Handle as obtained from BTA_HfClientOpen call
*
*
* Returns Control block corresponding to the handle and NULL if
* none exists
*
******************************************************************************/
tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_handle(uint16_t handle) {
// Handles are limited from 1 through HF_CLIENT_MAX_DEVICES
if (handle < 1 || handle > HF_CLIENT_MAX_DEVICES) {
APPL_TRACE_ERROR("%s: handle out of range (%d, %d) %d", __func__, 1,
HF_CLIENT_MAX_DEVICES, handle);
return NULL;
}
// Check if the associated index is allocated. Index is (handle - 1).
if (bta_hf_client_cb_arr.cb[handle - 1].is_allocated)
return &(bta_hf_client_cb_arr.cb[handle - 1]);
APPL_TRACE_ERROR("%s: block not found for handle %d", __func__, handle);
return NULL;
}
/*******************************************************************************
*
* Function bta_hf_client_find_cb_by_bda
*
* Description Finds the control block by handle provided
*
* bda: address of the device to find the handle for.
* Since there can only be one HF connection for a device
* we should always find a unique block
*
* Returns Control block corresponding to the address and NULL if
* none exists
*
******************************************************************************/
tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_bda(const RawAddress& peer_addr) {
for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
// Check if the associated index is allocated and that BD ADDR matches
tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
if (client_cb->is_allocated && peer_addr == client_cb->peer_addr) {
return client_cb;
} else {
APPL_TRACE_WARNING("%s: bdaddr mismatch for handle %d alloc %d", __func__,
i, client_cb->is_allocated);
}
}
APPL_TRACE_ERROR("%s: block not found", __func__);
return NULL;
}
/*******************************************************************************
*
* Function bta_hf_client_find_cb_by_rfc_handle
*
* Description Finds the control block by RFC handle provided.
*
* handle: RFC handle for the established connection
*
*
* Returns Control block corresponding to the handle and NULL if none
* exists
*
******************************************************************************/
tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_rfc_handle(uint16_t handle) {
for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
bool is_allocated = client_cb->is_allocated;
uint16_t conn_handle = client_cb->conn_handle;
APPL_TRACE_DEBUG("%s: cb rfc_handle %d alloc %d conn_handle %d", __func__,
handle, is_allocated, conn_handle);
if (is_allocated && conn_handle == handle) {
return client_cb;
}
APPL_TRACE_WARNING("%s: no cb yet %d alloc %d conn_handle %d", __func__,
handle, is_allocated, conn_handle);
}
APPL_TRACE_ERROR("%s: no cb found for rfc handle %d", __func__, handle);
return NULL;
}
/*******************************************************************************
*
* Function bta_hf_client_find_cb_by_sco_handle
*
* Description Finds the control block by sco handle provided
*
* handle: sco handle
*
*
* Returns Control block corresponding to the sco handle and
* none if none exists
*
******************************************************************************/
tBTA_HF_CLIENT_CB* bta_hf_client_find_cb_by_sco_handle(uint16_t handle) {
for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
if (client_cb->is_allocated && client_cb->sco_idx == handle) {
return client_cb;
}
}
APPL_TRACE_ERROR("%s: block not found for handle %d", __func__, handle);
return NULL;
}
/*******************************************************************************
*
* Function bta_hf_client_allocate_handle
*
* Description Allocates a handle for the new BD ADDR that needs a new RF
* channel for HF connection. If the channel cannot be created
* for a reason then false is returned
*
* bd_addr: Address of the device for which this block is
* being created. Single device can only have one block.
* p_handle: OUT variable to store the outcome of allocate. If
* allocate failed then value is not valid
*
*
* Returns true if the creation of p_handle succeeded, false otherwise
*
******************************************************************************/
bool bta_hf_client_allocate_handle(const RawAddress& bd_addr,
uint16_t* p_handle) {
tBTA_HF_CLIENT_CB* existing_cb = bta_hf_client_find_cb_by_bda(bd_addr);
if (existing_cb != NULL) {
BTIF_TRACE_ERROR("%s: cannot allocate handle since BDADDR already exists",
__func__);
return false;
}
/* Check that we do not have a request to for same device in the control
* blocks */
for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
if (client_cb->is_allocated) {
APPL_TRACE_WARNING("%s: control block already used index %d", __func__,
i);
continue;
}
// Reset the client control block
bta_hf_client_cb_init(client_cb, client_cb->handle);
*p_handle = client_cb->handle;
APPL_TRACE_DEBUG("%s: marking CB handle %d to true", __func__,
client_cb->handle);
client_cb->is_allocated = true;
client_cb->peer_addr = bd_addr;
bta_hf_client_at_init(client_cb);
return true;
}
return false;
APPL_TRACE_ERROR("%s: all control blocks in use!", __func__);
}
/*******************************************************************************
*
* Function bta_hf_client_app_callback
*
* Description Calls the application callback
*
*
* Returns Void
*
******************************************************************************/
void bta_hf_client_app_callback(uint16_t event, tBTA_HF_CLIENT* data) {
if (bta_hf_client_cb_arr.p_cback != NULL) {
bta_hf_client_cb_arr.p_cback(event, data);
}
}
/*******************************************************************************
*
* Function bta_hf_client_api_disable
*
* Description Handle an API disable event.
*
*
* Returns void
*
******************************************************************************/
void bta_hf_client_api_disable() {
if (!bta_sys_is_register(BTA_ID_HS)) {
APPL_TRACE_WARNING("BTA HF Client is already disabled, ignoring ...");
return;
}
/* Remove the collision handler */
bta_sys_collision_register(BTA_ID_HS, NULL);
bta_hf_client_cb_arr.deregister = true;
/* remove sdp record */
bta_hf_client_del_record(&bta_hf_client_cb_arr);
/* remove rfcomm server */
bta_hf_client_close_server();
/* reinit the control block */
for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
if (bta_hf_client_cb_arr.cb[i].is_allocated) {
bta_hf_client_cb_init(&(bta_hf_client_cb_arr.cb[i]), i);
}
}
/* De-register with BTA system manager */
bta_sys_deregister(BTA_ID_HS);
}
/*******************************************************************************
*
* Function bta_hf_client_hdl_event
*
* Description Data HF Client main event handling function.
*
*
* Returns bool
*
******************************************************************************/
bool bta_hf_client_hdl_event(BT_HDR_RIGID* p_msg) {
APPL_TRACE_DEBUG("%s: %s (0x%x)", __func__,
bta_hf_client_evt_str(p_msg->event), p_msg->event);
bta_hf_client_sm_execute(p_msg->event, (tBTA_HF_CLIENT_DATA*)p_msg);
return true;
}
/*******************************************************************************
*
* Function bta_hf_client_sm_execute
*
* Description State machine event handling function for HF Client
*
*
* Returns void
*
******************************************************************************/
void bta_hf_client_sm_execute(uint16_t event, tBTA_HF_CLIENT_DATA* p_data) {
tBTA_HF_CLIENT_CB* client_cb =
bta_hf_client_find_cb_by_handle(p_data->hdr.layer_specific);
if (client_cb == NULL) {
APPL_TRACE_ERROR("%s: cb not found for handle %d", __func__,
p_data->hdr.layer_specific);
return;
}
tBTA_HF_CLIENT_ST_TBL state_table;
uint8_t action;
int i;
uint16_t in_event = event;
uint8_t in_state = client_cb->state;
/* Ignore displaying of AT results when not connected (Ignored in state
* machine) */
if (client_cb->state == BTA_HF_CLIENT_OPEN_ST) {
APPL_TRACE_EVENT("HF Client evt : State %d (%s), Event 0x%04x (%s)",
client_cb->state,
bta_hf_client_state_str(client_cb->state), event,
bta_hf_client_evt_str(event));
}
event &= 0x00FF;
if (event >= (BTA_HF_CLIENT_MAX_EVT & 0x00FF)) {
APPL_TRACE_ERROR("HF Client evt out of range, ignoring...");
return;
}
/* look up the state table for the current state */
state_table = bta_hf_client_st_tbl[client_cb->state];
/* set next state */
client_cb->state = state_table[event][BTA_HF_CLIENT_NEXT_STATE];
/* execute action functions */
for (i = 0; i < BTA_HF_CLIENT_ACTIONS; i++) {
action = state_table[event][i];
if (action != BTA_HF_CLIENT_IGNORE) {
(*bta_hf_client_action[action])(p_data);
} else {
break;
}
}
/* If the state has changed then notify the app of the corresponding change */
if (in_state != client_cb->state) {
VLOG(1) << __func__ << ": notifying state change to " << in_state << " -> "
<< client_cb->state << " device " << client_cb->peer_addr;
tBTA_HF_CLIENT evt;
memset(&evt, 0, sizeof(evt));
evt.bd_addr = client_cb->peer_addr;
if (client_cb->state == BTA_HF_CLIENT_INIT_ST) {
bta_hf_client_app_callback(BTA_HF_CLIENT_CLOSE_EVT, &evt);
APPL_TRACE_DEBUG("%s: marking CB handle %d to false", __func__, client_cb->handle);
client_cb->is_allocated = false;
} else if (client_cb->state == BTA_HF_CLIENT_OPEN_ST) {
evt.open.handle = client_cb->handle;
bta_hf_client_app_callback(BTA_HF_CLIENT_OPEN_EVT, &evt);
}
}
VLOG(2) << __func__ << ": device " << client_cb->peer_addr
<< "state change: [" << bta_hf_client_state_str(in_state) << "] -> ["
<< bta_hf_client_state_str(client_cb->state) << "] after Event ["
<< bta_hf_client_evt_str(in_event) << "]";
}
static void send_post_slc_cmd(tBTA_HF_CLIENT_CB* client_cb) {
client_cb->at_cb.current_cmd = BTA_HF_CLIENT_AT_NONE;
tBTA_HF_CLIENT_DATA p_data;
p_data.hdr.layer_specific = client_cb->handle;
bta_hf_client_sco_listen(&p_data);
bta_hf_client_send_at_bia(client_cb);
bta_hf_client_send_at_ccwa(client_cb, true);
bta_hf_client_send_at_cmee(client_cb, true);
bta_hf_client_send_at_cops(client_cb, false);
bta_hf_client_send_at_btrh(client_cb, true, 0);
bta_hf_client_send_at_clip(client_cb, true);
}
/*******************************************************************************
*
* Function bta_hf_client_slc_seq
*
* Description Handles AT commands sequence required for SLC creation
*
*
* Returns void
*
******************************************************************************/
void bta_hf_client_slc_seq(tBTA_HF_CLIENT_CB* client_cb, bool error) {
APPL_TRACE_DEBUG("bta_hf_client_slc_seq cmd: %u",
client_cb->at_cb.current_cmd);
if (error) {
/* SLC establishment error, sent close rfcomm event */
APPL_TRACE_ERROR(
"HFPClient: Failed to create SLC due to AT error, disconnecting (%u)",
client_cb->at_cb.current_cmd);
tBTA_HF_CLIENT_DATA msg;
msg.hdr.layer_specific = client_cb->handle;
bta_hf_client_sm_execute(BTA_HF_CLIENT_API_CLOSE_EVT, &msg);
return;
}
if (client_cb->svc_conn) {
APPL_TRACE_WARNING("%s: SLC already connected for CB handle %d", __func__,
client_cb->handle);
return;
}
switch (client_cb->at_cb.current_cmd) {
case BTA_HF_CLIENT_AT_NONE:
bta_hf_client_send_at_brsf(client_cb, bta_hf_client_cb_arr.features);
break;
case BTA_HF_CLIENT_AT_BRSF:
if ((bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_CODEC) &&
(client_cb->peer_features & BTA_HF_CLIENT_PEER_CODEC)) {
bta_hf_client_send_at_bac(client_cb);
break;
}
bta_hf_client_send_at_cind(client_cb, false);
break;
case BTA_HF_CLIENT_AT_BAC:
bta_hf_client_send_at_cind(client_cb, false);
break;
case BTA_HF_CLIENT_AT_CIND:
bta_hf_client_send_at_cind(client_cb, true);
break;
case BTA_HF_CLIENT_AT_CIND_STATUS:
bta_hf_client_send_at_cmer(client_cb, true);
break;
case BTA_HF_CLIENT_AT_CMER:
if (client_cb->peer_features & BTA_HF_CLIENT_PEER_FEAT_3WAY &&
bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_3WAY) {
bta_hf_client_send_at_chld(client_cb, '?', 0);
} else if (bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_HF_IND &&
client_cb->peer_features & BTA_HF_CLIENT_PEER_HF_IND) {
bta_hf_client_send_at_bind(client_cb, 0);
} else {
tBTA_HF_CLIENT_DATA msg;
msg.hdr.layer_specific = client_cb->handle;
bta_hf_client_svc_conn_open(&msg);
send_post_slc_cmd(client_cb);
}
break;
case BTA_HF_CLIENT_AT_CHLD:
if (bta_hf_client_cb_arr.features & BTA_HF_CLIENT_FEAT_HF_IND &&
client_cb->peer_features & BTA_HF_CLIENT_PEER_HF_IND) {
bta_hf_client_send_at_bind(client_cb, 0);
} else {
tBTA_HF_CLIENT_DATA msg;
msg.hdr.layer_specific = client_cb->handle;
bta_hf_client_svc_conn_open(&msg);
send_post_slc_cmd(client_cb);
}
break;
case BTA_HF_CLIENT_AT_BIND_SET_IND:
bta_hf_client_send_at_bind(client_cb, 1);
break;
case BTA_HF_CLIENT_AT_BIND_READ_SUPPORTED_IND:
bta_hf_client_send_at_bind(client_cb, 2);
break;
case BTA_HF_CLIENT_AT_BIND_READ_ENABLED_IND:
tBTA_HF_CLIENT_DATA msg;
msg.hdr.layer_specific = client_cb->handle;
bta_hf_client_svc_conn_open(&msg);
send_post_slc_cmd(client_cb);
break;
default: {
/* If happen there is a bug in SLC creation procedure... */
APPL_TRACE_ERROR(
"HFPClient: Failed to create SLCdue to unexpected AT command, "
"disconnecting (%u)",
client_cb->at_cb.current_cmd);
tBTA_HF_CLIENT_DATA msg;
msg.hdr.layer_specific = client_cb->handle;
bta_hf_client_sm_execute(BTA_HF_CLIENT_API_CLOSE_EVT, &msg);
break;
}
}
}
#ifndef CASE_RETURN_STR
#define CASE_RETURN_STR(const) \
case const: \
return #const;
#endif
static const char* bta_hf_client_evt_str(uint16_t event) {
switch (event) {
CASE_RETURN_STR(BTA_HF_CLIENT_API_OPEN_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_API_CLOSE_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_API_AUDIO_OPEN_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_API_AUDIO_CLOSE_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_RFC_OPEN_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_RFC_CLOSE_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_RFC_SRV_CLOSE_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_RFC_DATA_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_DISC_ACP_RES_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_DISC_INT_RES_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_DISC_OK_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_DISC_FAIL_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_API_ENABLE_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_API_DISABLE_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_SCO_OPEN_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_SCO_CLOSE_EVT)
CASE_RETURN_STR(BTA_HF_CLIENT_SEND_AT_CMD_EVT)
default:
return "Unknown HF Client Event";
}
}
static const char* bta_hf_client_state_str(uint8_t state) {
switch (state) {
CASE_RETURN_STR(BTA_HF_CLIENT_INIT_ST)
CASE_RETURN_STR(BTA_HF_CLIENT_OPENING_ST)
CASE_RETURN_STR(BTA_HF_CLIENT_OPEN_ST)
CASE_RETURN_STR(BTA_HF_CLIENT_CLOSING_ST)
default:
return "Unknown HF Client State";
}
}
void bta_hf_client_dump_statistics(int fd) {
dprintf(fd, "\nBluetooth HF Client BTA Statistics\n");
// We dump statistics for all control blocks
for (int i = 0; i < HF_CLIENT_MAX_DEVICES; i++) {
tBTA_HF_CLIENT_CB* client_cb = &bta_hf_client_cb_arr.cb[i];
if (!client_cb->is_allocated) {
// Skip the blocks which are not allocated
continue;
}
dprintf(fd, " Control block #%d\n", i + 1);
uint8_t* a = client_cb->peer_addr.address;
// Device name
dprintf(fd, " Peer Device: %02x:%02x:%02x:%02x:%02x:%02x\n", a[0], a[1],
a[2], a[3], a[4], a[5]);
// State machine state
dprintf(fd, " State Machine State: %s\n",
bta_hf_client_state_str(client_cb->state));
// Local RFC channelfor communication
dprintf(fd, " RFCOMM Channel (local) %d\n", client_cb->conn_handle);
// BTA Handle shared between BTA and client (ex BTIF)
dprintf(fd, " BTA Generated handle %d\n", client_cb->handle);
}
}
|