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
|
/******************************************************************************
*
* Copyright (C) 2009-2012 Broadcom Corporation
*
* This program is the proprietary software of Broadcom Corporation and/or its
* licensors, and may only be used, duplicated, modified or distributed
* pursuant to the terms and conditions of a separate, written license
* agreement executed between you and Broadcom (an "Authorized License").
* Except as set forth in an Authorized License, Broadcom grants no license
* (express or implied), right to use, or waiver of any kind with respect to
* the Software, and Broadcom expressly reserves all rights in and to the
* Software and all intellectual property rights therein.
* IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU HAVE NO RIGHT TO USE THIS
* SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY NOTIFY BROADCOM AND DISCONTINUE
* ALL USE OF THE SOFTWARE.
*
* Except as expressly set forth in the Authorized License,
*
* 1. This program, including its structure, sequence and organization,
* constitutes the valuable trade secrets of Broadcom, and you shall
* use all reasonable efforts to protect the confidentiality thereof,
* and to use this information only in connection with your use of
* Broadcom integrated circuit products.
*
* 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED
* "AS IS" AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES,
* REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY,
* OR OTHERWISE, WITH RESPECT TO THE SOFTWARE. BROADCOM SPECIFICALLY
* DISCLAIMS ANY AND ALL IMPLIED WARRANTIES OF TITLE, MERCHANTABILITY,
* NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF VIRUSES,
* ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
* CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT
* OF USE OR PERFORMANCE OF THE SOFTWARE.
*
* 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR
* ITS LICENSORS BE LIABLE FOR
* (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR EXEMPLARY
* DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO
* YOUR USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM
* HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES; OR
* (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT ACTUALLY PAID FOR THE
* SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE
* LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF
* ESSENTIAL PURPOSE OF ANY LIMITED REMEDY.
*
******************************************************************************/
/******************************************************************************
*
* Filename: bte_main.c
*
* Description: Contains BTE core stack initialization and shutdown code
*
******************************************************************************/
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#include "gki.h"
#include "bd.h"
#include "btu.h"
#include "bte.h"
#include "bta_api.h"
#include "bt_hci_lib.h"
/*******************************************************************************
** Constants & Macros
*******************************************************************************/
/* Run-time configuration file */
#ifndef BTE_STACK_CONF_FILE
#define BTE_STACK_CONF_FILE "/etc/bluetooth/bt_stack.conf"
#endif
/* if not specified in .txt file then use this as default */
#ifndef HCI_LOGGING_FILENAME
#define HCI_LOGGING_FILENAME "/data/misc/bluedroid/btsnoop_hci.log"
#endif
/*******************************************************************************
** Local type definitions
*******************************************************************************/
/******************************************************************************
** Variables
******************************************************************************/
BOOLEAN hci_logging_enabled = FALSE; /* by default, turn hci log off */
char hci_logfile[256] = HCI_LOGGING_FILENAME;
/*******************************************************************************
** Static variables
*******************************************************************************/
static bt_hc_interface_t *bt_hc_if=NULL;
static const bt_hc_callbacks_t hc_callbacks;
static BOOLEAN lpm_enabled = FALSE;
/*******************************************************************************
** Static functions
*******************************************************************************/
static void bte_main_in_hw_init(void);
/*******************************************************************************
** Externs
*******************************************************************************/
BTU_API extern UINT32 btu_task (UINT32 param);
BTU_API extern void BTE_Init (void);
BT_API extern void BTE_LoadStack(void);
BT_API void BTE_UnloadStack(void);
extern void scru_flip_bda (BD_ADDR dst, const BD_ADDR src);
extern void bte_load_conf(const char *p_path);
/*******************************************************************************
** System Task Configuration
*******************************************************************************/
/* bluetooth protocol stack (BTU) task */
#ifndef BTE_BTU_STACK_SIZE
#define BTE_BTU_STACK_SIZE 0//0x2000 /* In bytes */
#endif
#define BTE_BTU_TASK_STR ((INT8 *) "BTU")
UINT32 bte_btu_stack[(BTE_BTU_STACK_SIZE + 3) / 4];
/******************************************************************************
**
** Function bte_main_in_hw_init
**
** Description Internal helper function for chip hardware init
**
** Returns None
**
******************************************************************************/
void bte_main_in_hw_init(void)
{
if ( (bt_hc_if = (bt_hc_interface_t *) bt_hc_get_interface()) \
== NULL)
{
APPL_TRACE_ERROR0("!!! Failed to get BtHostControllerInterface !!!");
}
}
/******************************************************************************
**
** Function bte_main_boot_entry
**
** Description BTE MAIN API - Entry point for BTE chip/stack initialization
**
** Returns None
**
******************************************************************************/
void bte_main_boot_entry(void)
{
/* initialize OS */
GKI_init();
bte_main_in_hw_init();
bte_load_conf(BTE_STACK_CONF_FILE);
#if (BTTRC_INCLUDED == TRUE)
/* Initialize trace feature */
BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
#endif
}
/******************************************************************************
**
** Function bte_main_shutdown
**
** Description BTE MAIN API - Shutdown code for BTE chip/stack
**
** Returns None
**
******************************************************************************/
void bte_main_shutdown()
{
GKI_shutdown();
}
/******************************************************************************
**
** Function bte_main_enable
**
** Description BTE MAIN API - Creates all the BTE tasks. Should be called
** part of the Bluetooth stack enable sequence
**
** Returns None
**
******************************************************************************/
void bte_main_enable(uint8_t *local_addr)
{
APPL_TRACE_DEBUG1("%s", __FUNCTION__);
/* Initialize BTE control block */
BTE_Init();
lpm_enabled = FALSE;
if (bt_hc_if)
{
int result = bt_hc_if->init(&hc_callbacks, local_addr);
APPL_TRACE_EVENT1("libbt-hci init returns %d", result);
assert(result == BT_HC_STATUS_SUCCESS);
if (hci_logging_enabled == TRUE)
bt_hc_if->logging(BT_HC_LOGGING_ON, hci_logfile);
#if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
APPL_TRACE_DEBUG1("%s Not Turninig Off the BT before Turninig ON", __FUNCTION__);
/* Do not power off the chip before powering on if BT_CLEAN_TURN_ON_DISABLED flag
is defined and set to TRUE to avoid below mentioned issue.
Wingray kernel driver maintains a combined counter to keep track of
BT-Wifi state. Invoking set_power(BT_HC_CHIP_PWR_OFF) when the BT is already
in OFF state causes this counter to be incorrectly decremented and results in undesired
behavior of the chip.
This is only a workaround and when the issue is fixed in the kernel this work around
should be removed. */
#else
/* toggle chip power to ensure we will reset chip in case
a previous stack shutdown wasn't completed gracefully */
bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF);
#endif
bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);
bt_hc_if->preload(NULL);
}
GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR,
(UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE),
sizeof(bte_btu_stack));
GKI_run(0);
}
/******************************************************************************
**
** Function bte_main_disable
**
** Description BTE MAIN API - Destroys all the BTE tasks. Should be called
** part of the Bluetooth stack disable sequence
**
** Returns None
**
******************************************************************************/
void bte_main_disable(void)
{
APPL_TRACE_DEBUG1("%s", __FUNCTION__);
if (bt_hc_if)
{
bt_hc_if->cleanup();
bt_hc_if->set_power(BT_HC_CHIP_PWR_OFF);
}
GKI_destroy_task(BTU_TASK);
GKI_freeze();
}
/******************************************************************************
**
** Function bte_main_postload_cfg
**
** Description BTE MAIN API - Stack postload configuration
**
** Returns None
**
******************************************************************************/
void bte_main_postload_cfg(void)
{
if (bt_hc_if)
bt_hc_if->postload(NULL);
}
#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
/******************************************************************************
**
** Function bte_main_enable_lpm
**
** Description BTE MAIN API - Enable/Disable low power mode operation
**
** Returns None
**
******************************************************************************/
void bte_main_enable_lpm(BOOLEAN enable)
{
int result = -1;
if (bt_hc_if)
result = bt_hc_if->lpm( \
(enable == TRUE) ? BT_HC_LPM_ENABLE : BT_HC_LPM_DISABLE \
);
APPL_TRACE_EVENT2("HC lib lpm enable=%d return %d", enable, result);
}
/******************************************************************************
**
** Function bte_main_lpm_allow_bt_device_sleep
**
** Description BTE MAIN API - Allow BT controller goest to sleep
**
** Returns None
**
******************************************************************************/
void bte_main_lpm_allow_bt_device_sleep()
{
int result = -1;
if ((bt_hc_if) && (lpm_enabled == TRUE))
result = bt_hc_if->lpm(BT_HC_LPM_WAKE_DEASSERT);
APPL_TRACE_DEBUG1("HC lib lpm deassertion return %d", result);
}
/******************************************************************************
**
** Function bte_main_lpm_wake_bt_device
**
** Description BTE MAIN API - Wake BT controller up if it is in sleep mode
**
** Returns None
**
******************************************************************************/
void bte_main_lpm_wake_bt_device()
{
int result = -1;
if ((bt_hc_if) && (lpm_enabled == TRUE))
result = bt_hc_if->lpm(BT_HC_LPM_WAKE_ASSERT);
APPL_TRACE_DEBUG1("HC lib lpm assertion return %d", result);
}
#endif // HCILP_INCLUDED
/******************************************************************************
**
** Function bte_main_hci_send
**
** Description BTE MAIN API - This function is called by the upper stack to
** send an HCI message. The function displays a protocol trace
** message (if enabled), and then calls the 'transmit' function
** associated with the currently selected HCI transport
**
** Returns None
**
******************************************************************************/
void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
{
UINT16 sub_event = event & BT_SUB_EVT_MASK; /* local controller ID */
p_msg->event = event;
if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
(sub_event == LOCAL_BLE_CONTROLLER_ID))
{
if (bt_hc_if)
bt_hc_if->transmit_buf((TRANSAC)p_msg, \
(char *) (p_msg + 1), \
p_msg->len);
else
GKI_freebuf(p_msg);
}
else
{
APPL_TRACE_ERROR0("Invalid Controller ID. Discarding message.");
GKI_freebuf(p_msg);
}
}
/******************************************************************************
**
** Function bte_main_post_reset_init
**
** Description BTE MAIN API - This function is mapped to BTM_APP_DEV_INIT
** and shall be automatically called from BTE after HCI_Reset
**
** Returns None
**
******************************************************************************/
void bte_main_post_reset_init()
{
BTM_ContinueReset();
}
/*****************************************************************************
**
** libbt-hci Callback Functions
**
*****************************************************************************/
/******************************************************************************
**
** Function preload_cb
**
** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
** when the libbt-hci completed stack preload process
**
** Returns None
**
******************************************************************************/
static void preload_cb(TRANSAC transac, bt_hc_preload_result_t result)
{
APPL_TRACE_EVENT1("HC preload_cb %d [0:SUCCESS 1:FAIL]", result);
/* notify BTU task that libbt-hci is ready */
/* even if PRELOAD process failed */
GKI_send_event(BTU_TASK, TASK_MBOX_0_EVT_MASK);
}
/******************************************************************************
**
** Function postload_cb
**
** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
** when the libbt-hci lib completed stack postload process
**
** Returns None
**
******************************************************************************/
static void postload_cb(TRANSAC transac, bt_hc_postload_result_t result)
{
APPL_TRACE_EVENT1("HC postload_cb %d", result);
}
/******************************************************************************
**
** Function lpm_cb
**
** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
** back from the libbt-hci to indicate the current LPM state
**
** Returns None
**
******************************************************************************/
static void lpm_cb(bt_hc_lpm_request_result_t result)
{
APPL_TRACE_EVENT1("HC lpm_result_cb %d", result);
lpm_enabled = (result == BT_HC_LPM_ENABLED) ? TRUE : FALSE;
}
/******************************************************************************
**
** Function hostwake_ind
**
** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
** from the libbt-hci to indicate the HostWake event
**
** Returns None
**
******************************************************************************/
static void hostwake_ind(bt_hc_low_power_event_t event)
{
APPL_TRACE_EVENT1("HC hostwake_ind %d", event);
}
/******************************************************************************
**
** Function alloc
**
** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
** from the libbt-hci to request for data buffer allocation
**
** Returns NULL / pointer to allocated buffer
**
******************************************************************************/
static char *alloc(int size)
{
BT_HDR *p_hdr = NULL;
/*
APPL_TRACE_DEBUG1("HC alloc size=%d", size);
*/
p_hdr = (BT_HDR *) GKI_getbuf ((UINT16) size);
if (p_hdr == NULL)
{
APPL_TRACE_WARNING0("alloc returns NO BUFFER!");
}
return ((char *) p_hdr);
}
/******************************************************************************
**
** Function dealloc
**
** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
** from the libbt-hci to release the data buffer allocated
** through the alloc call earlier
**
** Bluedroid libbt-hci library uses 'transac' parameter to
** pass data-path buffer/packet across bt_hci_lib interface
** boundary. The 'p_buf' is not intended to be used here
** but might point to data portion of data-path buffer.
**
** Returns bt_hc_status_t
**
******************************************************************************/
static int dealloc(TRANSAC transac, char *p_buf)
{
GKI_freebuf(transac);
return BT_HC_STATUS_SUCCESS;
}
/******************************************************************************
**
** Function data_ind
**
** Description HOST/CONTROLLER LIB CALLOUT API - This function is called
** from the libbt-hci to pass in the received HCI packets
**
** The core stack is responsible for releasing the data buffer
** passed in from the libbt-hci once the core stack has done
** with it.
**
** Bluedroid libbt-hci library uses 'transac' parameter to
** pass data-path buffer/packet across bt_hci_lib interface
** boundary. The 'p_buf' and 'len' parameters are not intended
** to be used here but might point to data portion in data-
** path buffer and length of valid data respectively.
**
** Returns bt_hc_status_t
**
******************************************************************************/
static int data_ind(TRANSAC transac, char *p_buf, int len)
{
BT_HDR *p_msg = (BT_HDR *) transac;
/*
APPL_TRACE_DEBUG2("HC data_ind event=0x%04X (len=%d)", p_msg->event, len);
*/
GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
return BT_HC_STATUS_SUCCESS;
}
/******************************************************************************
**
** Function tx_result
**
** Description HOST/CONTROLLER LIB CALLBACK API - This function is called
** from the libbt-hci once it has processed/sent the prior data
** buffer which core stack passed to it through transmit_buf
** call earlier.
**
** The core stack is responsible for releasing the data buffer
** if it has been completedly processed.
**
** Bluedroid libbt-hci library uses 'transac' parameter to
** pass data-path buffer/packet across bt_hci_lib interface
** boundary. The 'p_buf' is not intended to be used here
** but might point to data portion in data-path buffer.
**
** Returns bt_hc_status_t
**
******************************************************************************/
static int tx_result(TRANSAC transac, char *p_buf, \
bt_hc_transmit_result_t result)
{
/*
APPL_TRACE_DEBUG2("HC tx_result %d (event=%04X)", result, \
((BT_HDR *)transac)->event);
*/
if (result == BT_HC_TX_FRAGMENT)
{
GKI_send_msg (BTU_TASK, BTU_HCI_RCV_MBOX, transac);
}
else
{
GKI_freebuf(transac);
}
return BT_HC_STATUS_SUCCESS;
}
/*****************************************************************************
** The libbt-hci Callback Functions Table
*****************************************************************************/
static const bt_hc_callbacks_t hc_callbacks = {
sizeof(bt_hc_callbacks_t),
preload_cb,
postload_cb,
lpm_cb,
hostwake_ind,
alloc,
dealloc,
data_ind,
tx_result
};
|