Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger.
https://blog.csdn.net/huangweiqing80/article/details/82707399
bluetooth.c: enable
static int enable(bool start_restricted) {
LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
restricted_mode = start_restricted;
if (!interface_ready())
return BT_STATUS_NOT_READY;
stack_manager_get_interface()->start_up_stack_async();
return BT_STATUS_SUCCESS;
}
Call the protocol stack function: stack > manager > get > interface() - > Start > up > stack > async();
system/bt/btif/src/stack_manager.c
static void start_up_stack_async(void) {
thread_post(management_thread, event_start_up_stack, NULL);
}
static void event_start_up_stack(UNUSED_ATTR void *context) {
if (stack_is_running) {
LOG_DEBUG("%s stack already brought up.", __func__);
return;
}
ensure_stack_is_initialized();
LOG_DEBUG("%s is bringing up the stack.", __func__);
hack_future = future_new();
// Include this for now to put btif config into a shutdown-able state
module_start_up(get_module(BTIF_CONFIG_MODULE));
bte_main_enable();
if (future_await(hack_future) != FUTURE_SUCCESS) {
stack_is_running = true; // So stack shutdown actually happens
event_shut_down_stack(NULL);
return;
}
stack_is_running = true;
LOG_DEBUG("%s finished", __func__);
btif_thread_post(event_signal_stack_up, NULL);
}
system/bt/main/bte_main.c
/******************************************************************************
**
** 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()
{
APPL_TRACE_DEBUG("%s", __FUNCTION__);
module_start_up(get_module(BTSNOOP_MODULE));
module_start_up(get_module(HCI_MODULE));
BTU_StartUp();
}
system/bt/stack/btu/btu_init.c
/*****************************************************************************
**
** Function BTU_StartUp
**
** Description Initializes the BTU control block.
**
** NOTE: Must be called before creating any tasks
** (RPC, BTU, HCIT, APPL, etc.)
**
** Returns void
**
******************************************************************************/
void BTU_StartUp(void)
{
memset (&btu_cb, 0, sizeof (tBTU_CB));
btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
if (btu_bta_msg_queue == NULL)
goto error_exit;
btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
if (btu_general_alarm_hash_map == NULL)
goto error_exit;
if (pthread_mutex_init(&btu_general_alarm_lock, NULL))
goto error_exit;
btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
if (btu_general_alarm_queue == NULL)
goto error_exit;
btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
if (btu_oneshot_alarm_hash_map == NULL)
goto error_exit;
if (pthread_mutex_init(&btu_oneshot_alarm_lock, NULL))
goto error_exit;
btu_oneshot_alarm_queue = fixed_queue_new(SIZE_MAX);
if (btu_oneshot_alarm_queue == NULL)
goto error_exit;
btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
if (btu_l2cap_alarm_hash_map == NULL)
goto error_exit;
if (pthread_mutex_init(&btu_l2cap_alarm_lock, NULL))
goto error_exit;
btu_l2cap_alarm_queue = fixed_queue_new(SIZE_MAX);
if (btu_l2cap_alarm_queue == NULL)
goto error_exit;
bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
if (bt_workqueue_thread == NULL)
goto error_exit;
// Continue startup on bt workqueue thread.
thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
return;
error_exit:;
LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
BTU_ShutDown();
}
The BTU? Startup function is mainly used to
1. Initialize each message queue; for example, BTU BTA MSG queue = fixed queue new (size max);
2. Start worker thread: thread ﹣ post (BT ﹣ workqueue ﹣ thread, BTU ﹣ task ﹣ start ﹣ up, null);
Reference resources Introduction to threads in Bluedroid
So the BTU task start up function is called
system/bt/stack/btu/btu_task.c
void btu_task_start_up(UNUSED_ATTR void *context) {
BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
"btu_task pending for preload complete event");
LOG_INFO("Bluetooth chip preload is complete");
BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
"btu_task received preload complete event");
/* Initialize the mandatory core stack control blocks
(BTU, BTM, L2CAP, and SDP)
*/
btu_init_core();
/* Initialize any optional stack components */
BTE_InitStack();
bta_sys_init();
/* Initialise platform trace levels at this point as BTE_InitStack() and bta_sys_init()
* reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL
*/
#if ( BT_USE_TRACES==TRUE )
module_init(get_module(BTE_LOGMSG_MODULE));
#endif
// Inform the bt jni thread initialization is ok.
btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
fixed_queue_register_dequeue(btu_bta_msg_queue,
thread_get_reactor(bt_workqueue_thread),
btu_bta_msg_ready,
NULL);
fixed_queue_register_dequeue(btu_hci_msg_queue,
thread_get_reactor(bt_workqueue_thread),
btu_hci_msg_ready,
NULL);
fixed_queue_register_dequeue(btu_general_alarm_queue,
thread_get_reactor(bt_workqueue_thread),
btu_general_alarm_ready,
NULL);
fixed_queue_register_dequeue(btu_oneshot_alarm_queue,
thread_get_reactor(bt_workqueue_thread),
btu_oneshot_alarm_ready,
NULL);
fixed_queue_register_dequeue(btu_l2cap_alarm_queue,
thread_get_reactor(bt_workqueue_thread),
btu_l2cap_alarm_ready,
NULL);
}
Message queues and read threads are associated through the fixed queue register dequeue method. as
fixed_queue_register_dequeue(btu_bta_msg_queue,
thread_get_reactor(bt_workqueue_thread),
btu_bta_msg_ready,
NULL);
In this way, when a message is added to the BTU BTA MSG queue, that is, a message is sent to the BTU BTA MSG queue; BTU BTA MSG ready is called, and the BTU BTA MSG ready function is the message processing method
Similar to other queues, we can see that when btu task is started, the corresponding queues will be associated, such as BTU BTA MSG queue, BTU HCI MSG queue, BTU general alarm queue, BTU oneshot alarm queue, BTU L2CAP alarm queue