關鍵詞:bluedroid initNative enableNative BTIF_TASK BTU_TASKbt_hc_work_thread set_power preload GKI
作者:xubin341719(歡迎轉載,請註明作者,請尊重版權,謝謝!)
歡迎指正錯誤,共同學習、共同進步!!
一、 藍牙開啓流程概述,如下圖所示:init、enable
和一般的函數調用相同,android上層通過APP-->Native-->JNI-->bluetoothinterface-->bluetooth HCIinterface。HCI interface中實現了init、set_power、preload相應函數
init、enable函數主要實現的功能:
(1)、創建:btif_task/BTIF_TASK
(2)、初始化BTE
(3)、創建:btu_task/BTU_TASK
(4)、初始化HCI、串口相關,啓動HCI工作主線程:bt_hc_callback,芯片上電、RF參數初始化、藍牙地址名稱相關設定;
(5)、創建:bt_hc_worker_thread藍牙工作主線程,發送接收命令;
(6)、初始化藍牙協議棧;
二、initNative函數的的實現
這部分主要啓動相應sock、協議棧初始化、啓動btif_task,監聽處理藍牙接口相關的狀態消息。實現流程如下所示。
1、應用部分函數調用(從adatper開始)
packages\apps\Bluetooth\src\com\android\bluetooth\btservice\ AdapterService.java
public void onCreate() {
super.onCreate();
if (DBG) debugLog("onCreate");
mBinder = new AdapterServiceBinder(this);
mAdapterProperties = new AdapterProperties(this);
mAdapterStateMachine = AdapterState.make(this, mAdapterProperties);
mJniCallbacks = new JniCallbacks(mAdapterStateMachine, mAdapterProperties);
initNative();//調用initNative函數;
mNativeAvailable=true;
mCallbacks = new RemoteCallbackList<IBluetoothCallback>();
//Load the name and address
getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR);
getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME);
}
private native boolean initNative();
2、JNI函數的實現,這部分跟其他JNI實現相同。
packages\apps\Bluetooth\jni\com_android_bluetooth_btservice_AdapterService.cpp
static JNINativeMethod sMethods[] = {
/* name, signature, funcPtr */
{"classInitNative", "()V", (void *) classInitNative},
{"initNative", "()Z", (void *) initNative},//Native函數實現
…………
}
packages\apps\Bluetooth\jni\com_android_bluetooth_btservice_AdapterService.cpp
initNative函數的具體實現,通過bt_interface_t結構體,調用到C中的init函數實現。同時傳入sBluetoothCallbacks回調函數結構體。這個函數結構體比較重要,底層的狀態變化都是通過這個回調函數結構體中的函數實現。
static const bt_interface_t *sBluetoothInterface = NULL;
static bool initNative(JNIEnv* env, jobject obj) {
sJniCallbacksObj = env->NewGlobalRef(env->GetObjectField(obj, sJniCallbacksField));
if (sBluetoothInterface) {
int ret = sBluetoothInterface->init(&sBluetoothCallbacks);//調用到C的相應接口函數
if (ret != BT_STATUS_SUCCESS) {//如果出錯,錯誤處理;
ALOGE("Error while setting the callbacks \n");
sBluetoothInterface = NULL;
return JNI_FALSE;
}
if ( (sBluetoothSocketInterface = (btsock_interface_t *)
sBluetoothInterface->get_profile_interface(BT_PROFILE_SOCKETS_ID)) == NULL) {
ALOGE("Error getting socket interface");
}
return JNI_TRUE;
}
return JNI_FALSE;
}
3、JNI調用C中函數實現
C語言實現了上層調用的函數,最終實現了JAVA調用C函數的動作。這是android系統對kernel操作的具體步驟。如果是初學者,建議把這部分內容搞清楚,整個android系統對底層的操作都是通過這種方法實現。
external\bluetooth\bluedroid\btif\src\bluetooth.c
static const bt_interface_t bluetoothInterface = {//藍牙接口函數對應的函數
sizeof(bluetoothInterface),
init,//C函數中對init函數的實現;
enable,
disable,
cleanup,
get_adapter_properties,
get_adapter_property,
set_adapter_property,
get_remote_device_properties,
get_remote_device_property,
set_remote_device_property,
get_remote_service_record,
get_remote_services,
start_discovery,
cancel_discovery,
create_bond,
remove_bond,
cancel_bond,
pin_reply,
ssp_reply,
get_profile_interface,
dut_mode_configure,
dut_mode_send,
#if BLE_INCLUDED == TRUE
le_test_mode,
#else
NULL,
#endif
config_hci_snoop_log
};
4、藍牙接口函數中Init函數實現過程
external\bluetooth\bluedroid\btif\src\bluetooth.c
static int init(bt_callbacks_t* callbacks )
{
ALOGI("init");
/* sanity check */
if (interface_ready() == TRUE)//檢查接口函數是否準備好;
return BT_STATUS_DONE;
/* store reference to user callbacks */
bt_hal_cbacks = callbacks;//把相應的回調函數,保存,這個非常重要,剛開始看代碼是忽略這部分,我們單獨一節講解這部分回調函數的實現和作用;
/* add checks for individual callbacks ? */
bt_utils_init();//工具集初始化,初始化一個互斥鎖。
/* init btif */
btif_init_bluetooth();//初始化藍牙接口bluetoothinterface
return BT_STATUS_SUCCESS;
}
5、藍牙接口初始化具體實現,btif_init_bluetooth創建BTIF任務,準備藍牙開啓相關調度程序。
具體實現流程如下所示,我們下面對代碼做詳細解釋。主要完成了:
(1)、bt_config.xml文件中的藍牙名稱等處理;
(2)、GKI初始化,這部分後面單一節做詳細分析;
(3)、BlueHCLibInterface初始化,實現power\preload\等函數,BlueDreoid log等級設定;
(4)、BTIF_TASK線程創建,這個部分也比較重要。
external\bluetooth\bluedroid\btif\src\btif_core.c
bt_status_t btif_init_bluetooth()
{
UINT8 status;
btif_config_init();//創建sock線程,初始化初始化/data/misc/bluedroid/
bt_config.xml中相關數據;
bte_main_boot_entry();//(1)、BTE芯片協議棧入口API,藍牙協議棧/芯片初始化,GKI init;
/* As part of the init, fetch the local BD ADDR */
memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));//取藍牙地址寫入相關文件;
btif_fetch_local_bdaddr(&btif_local_bd_addr);
/* start btif task */
status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,
(UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),
sizeof(btif_task_stack));
//(2)、Creates BTIF task and prepares BT scheduler for startup 創建藍牙任務接口,爲開啓做調度準備
if (status != GKI_SUCCESS)
return BT_STATUS_FAIL;
return BT_STATUS_SUCCESS;
}
(1)、BTE芯片協議棧入口API,藍牙協議棧/芯片初始化,GKI init;
external\bluetooth\bluedroid\main\bte_main.c
void bte_main_boot_entry(void)
{
/* initialize OS */
GKI_init();//1)、GKI初始化,只在初始化的時候調用一次。
bte_main_in_hw_init();//2)、初始化結構體static bt_hc_interface_t *bt_hc_if=NULL;
bte_load_conf(BTE_STACK_CONF_FILE);//3)、初始化bluedroid調試信息等級;
#if (BTTRC_INCLUDED == TRUE)//相關打印信息初始化;
/* Initialize trace feature */
BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
#endif
}
1)、GKI初始化,只在初始化的時候調用一次
參考互斥鎖:http://blog.csdn.net/kingmax26/article/details/5338065 ;
external\bluetooth\bluedroid\gki\ulinux\gki_ulinux.c
void GKI_init(void)
{
pthread_mutexattr_t attr;
tGKI_OS *p_os;
memset (&gki_cb, 0, sizeof (gki_cb));
gki_buffer_init();//1))、GKI 緩衝、緩衝池初始化;
gki_timers_init();//2))、GKI定時器初始化;
gki_cb.com.OSTicks = (UINT32) times(0);
pthread_mutexattr_init(&attr);//3))、初始化pthread_mutexattr_t結構;
#ifndef __CYGWIN__
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);//4))、設置互斥鎖類型
#endif
p_os = &gki_cb.os;
pthread_mutex_init(&p_os->GKI_mutex, &attr);//5))、初始化互斥量GKI_mutex;
/* pthread_mutex_init(&GKI_sched_mutex, NULL); */
#if (GKI_DEBUG == TRUE)
pthread_mutex_init(&p_os->GKI_trace_mutex, NULL);// 6))、初始化互斥量GKI_trace_mutex;
#endif
/* pthread_mutex_init(&thread_delay_mutex, NULL); */ /* used in GKI_delay */
/* pthread_cond_init (&thread_delay_cond, NULL); */
/* Initialiase GKI_timer_update suspend variables & mutexes to be in running state.
* this works too even if GKI_NO_TICK_STOP is defined in btld.txt */
p_os->no_timer_suspend = GKI_TIMER_TICK_RUN_COND;
pthread_mutex_init(&p_os->gki_timer_mutex, NULL);7))、初始化互斥量gki_timer_mutex;
#ifndef NO_GKI_RUN_RETURN
pthread_cond_init(&p_os->gki_timer_cond, NULL);
#endif
}
2)、初始化結構體static bt_hc_interface_t *bt_hc_if=NULL;
bte_main_in_hw_init();
給bt_hc_if賦值:
static const bt_hc_interface_t bluetoothHCLibInterface = {
sizeof(bt_hc_interface_t),
init,//HCI LIB中init函數的實現;
set_power,
lpm,
preload,
postload,
transmit_buf,
set_rxflow,
logging,
cleanup
};
3)、初始化bluedroid調試信息等級
bte_load_conf(BTE_STACK_CONF_FILE);
解析bt_stack.conf文件中的配置信息。
(2)、創建藍牙任務接口,爲開啓做調度準備Creates BTIF task and prepares BT scheduler for startup
status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,
(UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),
sizeof(btif_task_stack));
6、btif_task進程相關處理函數
external\bluetooth\bluedroid\btif\src\btif_dm.c
btif_task 等待接收bta_sys_sendmsg發送的相應的狀態做相應處理。
static void btif_task(UINT32 params)
{
………………
for(;;)
{
/* wait for specified events */
event = GKI_wait(0xFFFF, 0);//GKI進程間通信後面單開一節介紹;
if (event == BT_EVT_TRIGGER_STACK_INIT)//協議棧初始化完成;
…………
if (event == BT_EVT_HARDWARE_INIT_FAIL)//硬件初始化錯誤
…………
if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))//收到關閉信息;
…………
if(event & TASK_MBOX_1_EVT_MASK)
…………
}
}