1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
72
73
74
75
76
77
78
87
88
89
98
99
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
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
233
234
235
236
237
238
242
243
244
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
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
503
504
507
508
509
510
511
512
513
514
515
516
517
518
519
520
525
526
527
528
529
530
531
532
533
534
535
536
537
541
542
543
544
545
546
547
548
549
550
551
552
553
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
579
580
581
582
583
584
585
586
587
588
589
590
591
598
599
600
601
602
603
604
605
608
609
610
611
615
616
617
618
619
620
621
622
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
730
731
738
739
740
741
742
743
744
745
746
747
748
749
753
754
755
756
757
758
759
760
761
762
763
/* ... */
/* ... */
#define LOG_TAG "bt_bta_sys_main"
#include <assert.h>
#include <string.h>
#include "osi/alarm.h"
#include "osi/thread.h"
#include "stack/btm_api.h"
#include "stack/btu.h"
#include "bta/bta_api.h"
#include "bta/bta_sys.h"
#include "bta_sys_int.h"
#include "osi/fixed_queue.h"
#include "osi/hash_map.h"
#include "osi/osi.h"
#include "osi/hash_functions.h"13 includes
#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
#include "bta/bta_ar_api.h"
#endif
#include "bta/utl.h"
#include "osi/allocator.h"
#include "osi/mutex.h"
#if BTA_DYNAMIC_MEMORY == FALSE
tBTA_SYS_CB bta_sys_cb;
#else
tBTA_SYS_CB *bta_sys_cb_ptr;
#endif
static hash_map_t *bta_alarm_hash_map;
static const size_t BTA_ALARM_HASH_MAP_SIZE = 17;
static osi_mutex_t bta_alarm_lock;
UINT8 appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
UINT8 btif_trace_level = BTIF_INITIAL_TRACE_LEVEL;
void btu_bta_alarm_ready(fixed_queue_t *queue);
static const tBTA_SYS_REG bta_sys_hw_reg = {
bta_sys_sm_execute,
NULL
}{...};
typedef void (*tBTA_SYS_ACTION)(tBTA_SYS_HW_MSG *p_data);
const tBTA_SYS_ACTION bta_sys_action[] = {
bta_sys_hw_api_enable,
bta_sys_hw_evt_enabled,
bta_sys_hw_evt_stack_enabled,
bta_sys_hw_api_disable,
bta_sys_hw_evt_disabled,
bta_sys_hw_error
}{...};
enum {
BTA_SYS_HW_API_ENABLE,
BTA_SYS_HW_EVT_ENABLED,
BTA_SYS_HW_EVT_STACK_ENABLED,
BTA_SYS_HW_API_DISABLE,
BTA_SYS_HW_EVT_DISABLED,
BTA_SYS_HW_ERROR
}{ ... };
#define BTA_SYS_NUM_ACTIONS (BTA_SYS_MAX_EVT & 0x00ff)
#define BTA_SYS_IGNORE BTA_SYS_NUM_ACTIONS
#define BTA_SYS_ACTIONS 2
#define BTA_SYS_NEXT_STATE 2
#define BTA_SYS_NUM_COLS 3 5 defines
const UINT8 bta_sys_hw_off[][BTA_SYS_NUM_COLS] = {
{BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
{BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF}
}{...};
const UINT8 bta_sys_hw_starting[][BTA_SYS_NUM_COLS] = {
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
{BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
{BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING},
{BTA_SYS_HW_EVT_DISABLED, BTA_SYS_HW_API_ENABLE, BTA_SYS_HW_STARTING},
{BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}
}{...};
const UINT8 bta_sys_hw_on[][BTA_SYS_NUM_COLS] = {
{BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
{BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
{BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
{BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}
}{...};
const UINT8 bta_sys_hw_stopping[][BTA_SYS_NUM_COLS] = {
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
{BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING},
{BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_HW_API_DISABLE, BTA_SYS_HW_STOPPING},
{BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING},
{BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
{BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}
}{...};
typedef const UINT8 (*tBTA_SYS_ST_TBL)[BTA_SYS_NUM_COLS];
const tBTA_SYS_ST_TBL bta_sys_st_tbl[] = {
bta_sys_hw_off,
bta_sys_hw_starting,
bta_sys_hw_on,
bta_sys_hw_stopping
}{...};
/* ... */
void bta_sys_init(void)
{
memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
osi_mutex_new(&bta_alarm_lock);
bta_alarm_hash_map = hash_map_new(BTA_ALARM_HASH_MAP_SIZE,
hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
bta_sys_register( BTA_ID_SYS, &bta_sys_hw_reg);
BTM_RegisterForDeviceStatusNotif ((tBTM_DEV_STATUS_CB *)&bta_sys_hw_btm_cback );
#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
bta_ar_init();
#endif
}{ ... }
void bta_sys_free(void)
{
hash_map_free(bta_alarm_hash_map);
osi_mutex_free(&bta_alarm_lock);
#if BTA_DYNAMIC_MEMORY
FREE_AND_RESET(bta_sys_cb_ptr);
#endif
}{ ... }
/* ... */
BOOLEAN bta_sys_sm_execute(BT_HDR *p_msg)
{
BOOLEAN freebuf = TRUE;
tBTA_SYS_ST_TBL state_table;
UINT8 action;
int i;
APPL_TRACE_EVENT("bta_sys_sm_execute state:%d, event:0x%x\n", bta_sys_cb.state, p_msg->event);
state_table = bta_sys_st_tbl[bta_sys_cb.state];
bta_sys_cb.state = state_table[p_msg->event & 0x00ff][BTA_SYS_NEXT_STATE];
for (i = 0; i < BTA_SYS_ACTIONS; i++) {
if ((action = state_table[p_msg->event & 0x00ff][i]) != BTA_SYS_IGNORE) {
(*bta_sys_action[action])( (tBTA_SYS_HW_MSG *) p_msg);
}{...} else {
break;
}{...}
}{...}
return freebuf;
}{ ... }
void bta_sys_hw_register( tBTA_SYS_HW_MODULE module, tBTA_SYS_HW_CBACK *cback)
{
bta_sys_cb.sys_hw_cback[module] = cback;
}{ ... }
void bta_sys_hw_unregister( tBTA_SYS_HW_MODULE module )
{
bta_sys_cb.sys_hw_cback[module] = NULL;
}{ ... }
/* ... */
void bta_sys_hw_btm_cback( tBTM_DEV_STATUS status )
{
tBTA_SYS_HW_MSG *sys_event;
APPL_TRACE_DEBUG(" bta_sys_hw_btm_cback was called with parameter: %i" , status );
if ((sys_event = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
if (status == BTM_DEV_STATUS_UP) {
sys_event->hdr.event = BTA_SYS_EVT_STACK_ENABLED_EVT;
}{...} else if (status == BTM_DEV_STATUS_DOWN) {
sys_event->hdr.event = BTA_SYS_ERROR_EVT;
}{...} else {
osi_free (sys_event);
sys_event = NULL;
}{...}
if (sys_event) {
bta_sys_sendmsg(sys_event);
}{...}
}{...} else {
APPL_TRACE_DEBUG("ERROR bta_sys_hw_btm_cback couldn't send msg" );
}{...}
}{ ... }
/* ... */
void bta_sys_hw_error(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
UINT8 module_index;
UNUSED(p_sys_hw_msg);
APPL_TRACE_DEBUG("%s\n", __FUNCTION__);
for (module_index = 0; module_index < BTA_SYS_MAX_HW_MODULES; module_index++) {
if ( bta_sys_cb.sys_hw_module_active & ((UINT32)1 << module_index )) {
switch ( module_index) {
case BTA_SYS_HW_BLUETOOTH:
if (bta_sys_cb.sys_hw_cback[module_index] != NULL) {
bta_sys_cb.sys_hw_cback[module_index] (BTA_SYS_HW_ERROR_EVT);
}{...}
break;...
default:
break;...
}{...}
}{...}
}{...}
}{ ... }
/* ... */
void bta_sys_hw_api_enable( tBTA_SYS_HW_MSG *p_sys_hw_msg )
{
if ((!bta_sys_cb.sys_hw_module_active) && (bta_sys_cb.state != BTA_SYS_HW_ON)) {
bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
tBTA_SYS_HW_MSG *p_msg;
if ((p_msg = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
p_msg->hdr.event = BTA_SYS_EVT_ENABLED_EVT;
p_msg->hw_module = p_sys_hw_msg->hw_module;
bta_sys_sendmsg(p_msg);
}{...}
}{...} else {
bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ] != NULL ) {
bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_ON_EVT );
}{...}
}{...}
APPL_TRACE_EVENT ("bta_sys_hw_api_enable for %d, active modules 0x%04X\n",
p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
}{ ... }
/* ... */
void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
APPL_TRACE_DEBUG("bta_sys_hw_api_disable for %d, active modules: 0x%04X\n",
p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active );
bta_sys_disable( p_sys_hw_msg->hw_module );
bta_sys_cb.sys_hw_module_active &= ~((UINT32)1 << p_sys_hw_msg->hw_module );
if ( bta_sys_cb.sys_hw_module_active != 0 ) {
if ( bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ] != NULL ) {
bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_OFF_EVT );
}{...}
}{...} else {
bta_sys_cb.state = BTA_SYS_HW_STOPPING;
tBTA_SYS_HW_MSG *p_msg;
if ((p_msg = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
p_msg->hdr.event = BTA_SYS_EVT_DISABLED_EVT;
p_msg->hw_module = p_sys_hw_msg->hw_module;
bta_sys_sendmsg(p_msg);
}{...}
}{...}
}{ ... }
/* ... */
void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
APPL_TRACE_EVENT("bta_sys_hw_evt_enabled for %i\n", p_sys_hw_msg->hw_module);
BTM_DeviceReset( NULL );
}{ ... }
/* ... */
void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
UINT8 hw_module_index;
APPL_TRACE_DEBUG("bta_sys_hw_evt_disabled - module 0x%X\n", p_sys_hw_msg->hw_module);
for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++) {
if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL) {
bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_OFF_EVT);
}{...}
}{...}
}{ ... }
/* ... */
void bta_sys_hw_evt_stack_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
{
UINT8 hw_module_index;
UNUSED(p_sys_hw_msg);
APPL_TRACE_DEBUG(" bta_sys_hw_evt_stack_enabled!notify the callers\n");
for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++ ) {
if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL) {
bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_ON_EVT);
}{...}
}{...}
}{ ... }
/* ... */
void bta_sys_event(void * param)
{
BT_HDR *p_msg = (BT_HDR *)param;
UINT8 id;
BOOLEAN freebuf = TRUE;
APPL_TRACE_EVENT("BTA got event 0x%x\n", p_msg->event);
id = (UINT8) (p_msg->event >> 8);
if ((id < BTA_ID_MAX) && (bta_sys_cb.reg[id] != NULL)) {
freebuf = (*bta_sys_cb.reg[id]->evt_hdlr)(p_msg);
}{...} else {
APPL_TRACE_WARNING("BTA got unregistered event id %d\n", id);
}{...}
if (freebuf) {
osi_free(p_msg);
}{...}
}{ ... }
/* ... */
void bta_sys_register(UINT8 id, const tBTA_SYS_REG *p_reg)
{
bta_sys_cb.reg[id] = (tBTA_SYS_REG *) p_reg;
bta_sys_cb.is_reg[id] = TRUE;
}{ ... }
/* ... */
void bta_sys_deregister(UINT8 id)
{
bta_sys_cb.is_reg[id] = FALSE;
}{ ... }
/* ... */
BOOLEAN bta_sys_is_register(UINT8 id)
{
return bta_sys_cb.is_reg[id];
}{ ... }
/* ... */
void bta_sys_sendmsg(void *p_msg)
{
if (btu_task_post(SIG_BTU_BTA_MSG, p_msg, OSI_THREAD_MAX_TIMEOUT) == false) {
osi_free(p_msg);
}{...}
}{ ... }
/* ... */
void bta_alarm_cb(void *data)
{
assert(data != NULL);
TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
btu_task_post(SIG_BTU_BTA_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
}{ ... }
void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms)
{
assert(p_tle != NULL);
osi_mutex_lock(&bta_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
if (!hash_map_has_key(bta_alarm_hash_map, p_tle)) {
hash_map_set(bta_alarm_hash_map, p_tle, osi_alarm_new("bta_sys", bta_alarm_cb, p_tle, 0));
}{...}
osi_mutex_unlock(&bta_alarm_lock);
osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
if (alarm == NULL) {
APPL_TRACE_ERROR("%s unable to create alarm.", __func__);
return;
}{...}
p_tle->event = type;
p_tle->ticks = timeout_ms;
osi_alarm_set(alarm, (period_ms_t)timeout_ms);
}{ ... }
bool hash_iter_ro_cb(hash_map_entry_t *hash_map_entry, void *context)
{
osi_alarm_t *alarm = (osi_alarm_t *)hash_map_entry->data;
period_ms_t *p_remaining_ms = (period_ms_t *)context;
*p_remaining_ms += osi_alarm_get_remaining_ms(alarm);
return true;
}{ ... }
UINT32 bta_sys_get_remaining_ticks(TIMER_LIST_ENT *p_target_tle)
{
period_ms_t remaining_ms = 0;
osi_mutex_lock(&bta_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
hash_map_foreach(bta_alarm_hash_map, hash_iter_ro_cb, &remaining_ms);
osi_mutex_unlock(&bta_alarm_lock);
return remaining_ms;
}{ ... }
/* ... */
BOOLEAN bta_sys_timer_is_active(TIMER_LIST_ENT *p_tle)
{
assert(p_tle != NULL);
osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
if (alarm != NULL && osi_alarm_is_active(alarm)) {
return TRUE;
}{...}
return FALSE;
}{ ... }
/* ... */
void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle)
{
assert(p_tle != NULL);
osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
if (alarm == NULL) {
APPL_TRACE_DEBUG("%s expected alarm was not in bta alarm hash map.", __func__);
return;
}{...}
osi_alarm_cancel(alarm);
}{ ... }
/* ... */
void bta_sys_free_timer(TIMER_LIST_ENT *p_tle)
{
assert(p_tle != NULL);
osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
if (alarm == NULL) {
APPL_TRACE_DEBUG("%s expected alarm was not in bta alarm hash map.", __func__);
return;
}{...}
osi_alarm_cancel(alarm);
hash_map_erase(bta_alarm_hash_map, p_tle);
}{ ... }
/* ... */
void bta_sys_disable(tBTA_SYS_HW_MODULE module)
{
int bta_id = 0;
int bta_id_max = 0;
APPL_TRACE_DEBUG("bta_sys_disable: module %i", module);
switch ( module ) {
case BTA_SYS_HW_BLUETOOTH:
bta_id = BTA_ID_DM;
bta_id_max = BTA_ID_BLUETOOTH_MAX;
break;...
default:
APPL_TRACE_WARNING("bta_sys_disable: unkown module");
return;...
}{...}
for ( ; bta_id <= bta_id_max; bta_id++) {
if (bta_sys_cb.reg[bta_id] != NULL) {
if (bta_sys_cb.is_reg[bta_id] == TRUE && bta_sys_cb.reg[bta_id]->disable != NULL) {
(*bta_sys_cb.reg[bta_id]->disable)();
}{...}
}{...}
}{...}
}{ ... }
/* ... */
void bta_sys_set_trace_level(UINT8 level)
{
appl_trace_level = level;
}{ ... }
/* ... */
UINT16 bta_sys_get_sys_features (void)
{
return bta_sys_cb.sys_features;
}{ ... }