Android BlueDroid(二):BlueDroid蓝牙开启过程init

来源:互联网 发布:淘宝店网址花钱改 编辑:程序博客网 时间:2024/05/15 14:23


关键词: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

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.     public void onCreate() {  
  2.         super.onCreate();  
  3.         if (DBG) debugLog("onCreate");  
  4.         mBinder = new AdapterServiceBinder(this);  
  5.         mAdapterProperties = new AdapterProperties(this);  
  6.         mAdapterStateMachine =  AdapterState.make(this, mAdapterProperties);  
  7.         mJniCallbacks =  new JniCallbacks(mAdapterStateMachine, mAdapterProperties);  
  8.       <span style="color: rgb(255, 0, 0);"> </span> initNative();//调用initNative函数;  
  9.         mNativeAvailable=true;  
  10.         mCallbacks = new RemoteCallbackList<IBluetoothCallback>();  
  11.         //Load the name and address  
  12.         getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDADDR);  
  13.         getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_BDNAME);  
  14. }  
  15. private native boolean initNative();  

2、JNI函数的实现,这部分跟其他JNI实现相同。
packages\apps\Bluetooth\jni\com_android_bluetooth_btservice_AdapterService.cpp

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. static JNINativeMethod sMethods[] = {  
  2.     /* name, signature, funcPtr */  
  3.     {"classInitNative""()V", (void *) classInitNative},  
  4.     {"initNative""()Z", (void *) initNative},//Native函数实现  
  5. …………  
  6. }<strong>  
  7. </strong>  

packages\apps\Bluetooth\jni\com_android_bluetooth_btservice_AdapterService.cpp
initNative函数的具体实现,通过bt_interface_t结构体,调用到C中的init函数实现。同时传入sBluetoothCallbacks回调函数结构体。这个函数结构体比较重要,底层的状态变化都是通过这个回调函数结构体中的函数实现。

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. static const bt_interface_t *sBluetoothInterface = NULL;  
  2. static bool initNative(JNIEnv* env, jobject obj) {  
  3.     sJniCallbacksObj = env->NewGlobalRef(env->GetObjectField(obj, sJniCallbacksField));  
  4.     if (sBluetoothInterface) {  
  5.         int ret = sBluetoothInterface->init(&sBluetoothCallbacks);//调用到C的相应接口函数  
  6.         if (ret != BT_STATUS_SUCCESS) {//如果出错,错误处理;  
  7.             ALOGE("Error while setting the callbacks \n");  
  8.             sBluetoothInterface = NULL;  
  9.             return JNI_FALSE;  
  10.         }  
  11.         if ( (sBluetoothSocketInterface = (btsock_interface_t *)  
  12.                   sBluetoothInterface->get_profile_interface(BT_PROFILE_SOCKETS_ID)) == NULL) {  
  13.                 ALOGE("Error getting socket interface");  
  14.         }  
  15.         return JNI_TRUE;  
  16.     }  
  17.     return JNI_FALSE;  
  18. }  

3、JNI调用C中函数实现
C语言实现了上层调用的函数,最终实现了JAVA调用C函数的动作。这是android系统对kernel操作的具体步骤。如果是初学者,建议把这部分内容搞清楚,整个android系统对底层的操作都是通过这种方法实现。
external\bluetooth\bluedroid\btif\src\bluetooth.c

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. static const bt_interface_t bluetoothInterface = {//蓝牙接口函数对应的函数  
  2.     sizeof(bluetoothInterface),  
  3.     init,//C函数中对init函数的实现;  
  4.     enable,  
  5.     disable,  
  6.     cleanup,  
  7.     get_adapter_properties,  
  8.     get_adapter_property,  
  9.     set_adapter_property,  
  10.     get_remote_device_properties,  
  11.     get_remote_device_property,  
  12.     set_remote_device_property,  
  13.     get_remote_service_record,  
  14.     get_remote_services,  
  15.     start_discovery,  
  16.     cancel_discovery,  
  17.     create_bond,  
  18.     remove_bond,  
  19.     cancel_bond,  
  20.     pin_reply,  
  21.     ssp_reply,  
  22.     get_profile_interface,  
  23.     dut_mode_configure,  
  24.     dut_mode_send,  
  25. #if BLE_INCLUDED == TRUE  
  26.     le_test_mode,  
  27. #else  
  28.     NULL,  
  29. #endif  
  30.     config_hci_snoop_log  
  31. };  

4、蓝牙接口函数中Init函数实现过程
external\bluetooth\bluedroid\btif\src\bluetooth.c

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. static int init(bt_callbacks_t* callbacks )  
  2. {  
  3.     ALOGI("init");  
  4.     /* sanity check */  
  5.     if (interface_ready() == TRUE)//检查接口函数是否准备好;  
  6.         return BT_STATUS_DONE;  
  7.     /* store reference to user callbacks */  
  8.     bt_hal_cbacks = callbacks;//把相应的回调函数,保存,这个非常重要,刚开始看代码是忽略这部分,我们单独一节讲解这部分回调函数的实现和作用;  
  9.     /* add checks for individual callbacks ? */  
  10.     bt_utils_init();//工具集初始化,初始化一个互斥锁。  
  11.     /* init btif */  
  12. btif_init_bluetooth();//初始化蓝牙接口bluetoothinterface  
  13.     return BT_STATUS_SUCCESS;  
  14. }  

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

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. bt_status_t btif_init_bluetooth()  
  2. {  
  3.     UINT8 status;  
  4.     btif_config_init();//创建sock线程,初始化初始化/data/misc/bluedroid/  
  5. bt_config.xml中相关数据;  
  6.     bte_main_boot_entry();//(1)、BTE芯片协议栈入口API,蓝牙协议栈/芯片初始化,GKI init;  
  7.     /* As part of the init, fetch the local BD ADDR */  
  8.     memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));//取蓝牙地址写入相关文件;  
  9.     btif_fetch_local_bdaddr(&btif_local_bd_addr);  
  10.     /* start btif task */  
  11.     status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,  
  12.                 (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),  
  13.                 sizeof(btif_task_stack));  
  14. //(2)、Creates BTIF task and prepares BT scheduler for startup  创建蓝牙任务接口,为开启做调度准备  
  15.     if (status != GKI_SUCCESS)  
  16.         return BT_STATUS_FAIL;  
  17.     return BT_STATUS_SUCCESS;  
  18. }  

(1)、BTE芯片协议栈入口API,蓝牙协议栈/芯片初始化,GKI init;
external\bluetooth\bluedroid\main\bte_main.c

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void bte_main_boot_entry(void)  
  2. {  
  3.     /* initialize OS */  
  4.     GKI_init();//1)、GKI初始化,只在初始化的时候调用一次。  
  5.   
  6.     bte_main_in_hw_init();//2)、初始化结构体static bt_hc_interface_t *bt_hc_if=NULL;  
  7.   
  8.     bte_load_conf(BTE_STACK_CONF_FILE);//3)、初始化bluedroid调试信息等级;  
  9.   
  10. #if (BTTRC_INCLUDED == TRUE)//相关打印信息初始化;  
  11.     /* Initialize trace feature */  
  12.     BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);  
  13. #endif  
  14. }  


1)、GKI初始化,只在初始化的时候调用一次
参考互斥锁:http://blog.csdn.net/kingmax26/article/details/5338065  ;
external\bluetooth\bluedroid\gki\ulinux\gki_ulinux.c

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. void GKI_init(void)  
  2. {  
  3.     pthread_mutexattr_t attr;  
  4.     tGKI_OS             *p_os;  
  5.   
  6.     memset (&gki_cb, 0, sizeof (gki_cb));  
  7.   
  8.     gki_buffer_init();//1))、GKI 缓冲、缓冲池初始化;  
  9.     gki_timers_init();//2))、GKI定时器初始化;  
  10.     gki_cb.com.OSTicks = (UINT32) times(0);  
  11.   
  12.     pthread_mutexattr_init(&attr);//3))、初始化pthread_mutexattr_t结构;  
  13.   
  14. #ifndef __CYGWIN__  
  15.     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);//4))、设置互斥锁类型  
  16. #endif  
  17.     p_os = &gki_cb.os;  
  18.     pthread_mutex_init(&p_os->GKI_mutex, &attr);//5))、初始化互斥量GKI_mutex;  
  19.     /* pthread_mutex_init(&GKI_sched_mutex, NULL); */  
  20. #if (GKI_DEBUG == TRUE)  
  21.     pthread_mutex_init(&p_os->GKI_trace_mutex, NULL);//  6))、初始化互斥量GKI_trace_mutex;  
  22. #endif  
  23.     /* pthread_mutex_init(&thread_delay_mutex, NULL); */  /* used in GKI_delay */  
  24.     /* pthread_cond_init (&thread_delay_cond, NULL); */  
  25.   
  26.     /* Initialiase GKI_timer_update suspend variables & mutexes to be in running state.  
  27.      * this works too even if GKI_NO_TICK_STOP is defined in btld.txt */  
  28.     p_os->no_timer_suspend = GKI_TIMER_TICK_RUN_COND;  
  29.     pthread_mutex_init(&p_os->gki_timer_mutex, NULL);7))、初始化互斥量gki_timer_mutex;  
  30. #ifndef NO_GKI_RUN_RETURN  
  31.     pthread_cond_init(&p_os->gki_timer_cond, NULL);  
  32. #endif  
  33. }  

2)、初始化结构体static bt_hc_interface_t *bt_hc_if=NULL;
bte_main_in_hw_init();
给bt_hc_if赋值:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. static const bt_hc_interface_t bluetoothHCLibInterface = {  
  2.     sizeof(bt_hc_interface_t),  
  3.     init,//HCI LIB中init函数的实现;  
  4.     set_power,  
  5.     lpm,  
  6.     preload,  
  7.     postload,  
  8.     transmit_buf,  
  9.     set_rxflow,  
  10.     logging,  
  11.     cleanup  
  12. };  

3)、初始化bluedroid调试信息等级
    bte_load_conf(BTE_STACK_CONF_FILE);
解析bt_stack.conf文件中的配置信息。
(2)、创建蓝牙任务接口,为开启做调度准备Creates BTIF task and prepares BT scheduler for startup  

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,  
  2.               (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),  
  3.               sizeof(btif_task_stack));  

6、btif_task进程相关处理函数
external\bluetooth\bluedroid\btif\src\btif_dm.c
btif_task 等待接收bta_sys_sendmsg发送的相应的状态做相应处理。

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. static void btif_task(UINT32 params)  
  2. {  
  3. ………………  
  4.     for(;;)  
  5. {  
  6.         /* wait for specified events */  
  7.         event = GKI_wait(0xFFFF, 0);//GKI进程间通信后面单开一节介绍;  
  8.         if (event == BT_EVT_TRIGGER_STACK_INIT)//协议栈初始化完成;  
  9. …………  
  10.         if (event == BT_EVT_HARDWARE_INIT_FAIL)//硬件初始化错误  
  11. …………  
  12.         if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))//收到关闭信息;  
  13.  …………  
  14.         if(event & TASK_MBOX_1_EVT_MASK)  
  15. …………  
  16. }  
  17. }  

    
0 0
原创粉丝点击