Android BlueDroid(二):BlueDroid藍芽開啟過程init

來源:互聯網
上載者:User

標籤:bluetooth   藍芽   android   

關鍵詞: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)…………}}

Android BlueDroid(二):BlueDroid藍芽開啟過程init

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.