当前位置:   article > 正文

BlueDroid代码分析之初始化_bluedroid的库

bluedroid的库

安卓蓝牙协议栈初始化

bluedroid协议栈初始化过程如下图所示:

Bluetooth.c是安卓蓝牙的硬件抽象,是BlueDroid的对外接口,供JNI直接调用。bluedroid编译出的库为bluetooth.default.so依赖于libbt-hci.so、libbt-utils.so、libbt-vendor.so等动态库。下面逐一分析安卓是怎么初始化协议栈的!本文是从协议栈bluedroid角度分析的,安卓的framework层次的初始化请参考 

安卓蓝牙协议栈的初始化-java层次

从上图可以看到,协议栈的初始化和enable过程创建了sock_poll_thread ,bt_hc_worker_thread,
btif_task,btu_task.sock_poll_thread跟蓝牙多媒体相关,bt_hc_worker_thread是串口的发送/接收数据进程,btif_task处理用户提交的命令,与HAL 和BTA交互进程,btu_task与L2CAP交互进程。下面我们着重分析下后面的两个进程:
在init的时候会创建btif_task进程,代码如下:
btif/src/btif_core.c
  1. bt_status_t btif_init_bluetooth()
  2. {
  3. UINT8 status;
  4. btif_config_init();//创建<span style="font-size: 18px;">sock_poll_thread,并获取配置文件</span>
  5. bte_main_boot_entry();//初始化GKI,加载厂商库和相关配置文件
  6. /* As part of the init, fetch the local BD ADDR */
  7. memset(&btif_local_bd_addr, 0, sizeof(bt_bdaddr_t));
  8. btif_fetch_local_bdaddr(&btif_local_bd_addr); //从配置文件获取本地蓝牙的mac地址
  9. /* start btif task */
  10. status = GKI_create_task(btif_task, BTIF_TASK, BTIF_TASK_STR,
  11. (UINT16 *) ((UINT8 *)btif_task_stack + BTIF_TASK_STACK_SIZE),
  12. sizeof(btif_task_stack)); //创建btif_task进程
  13. if (status != GKI_SUCCESS)
  14. return BT_STATUS_FAIL;
  15. return BT_STATUS_SUCCESS;
  16. }
btif_task代码路径:
btif/src/btif_core.c
  1. static void btif_task(UINT32 params)
  2. {
  3. UINT16 event;
  4. BT_HDR *p_msg;
  5. BTIF_TRACE_DEBUG0("btif task starting");
  6. btif_associate_evt();//回调注册的thread_evt_cb,通知ASSOCIATE_JVM事件
  7. for(;;)
  8. {
  9. /* wait for specified events */
  10. event = GKI_wait(0xFFFF, 0); //等待有效的事件,或者有消息发送到本进程时
  11. //在GKI_send_msg最后也会发送event到本线程
  12. /*
  13. * Wait for the trigger to init chip and stack. This trigger will
  14. * be received by btu_task once the UART is opened and ready
  15. */
  16. if (event == BT_EVT_TRIGGER_STACK_INIT)
  17. {
  18. BTIF_TRACE_DEBUG0("btif_task: received trigger stack init event");
  19. #if (BLE_INCLUDED == TRUE)
  20. btif_dm_load_ble_local_keys();
  21. #endif
  22. BTA_EnableBluetooth(bte_dm_evt);
  23. }
  24. /*
  25. * Failed to initialize controller hardware, reset state and bring
  26. * down all threads
  27. */
  28. if (event == BT_EVT_HARDWARE_INIT_FAIL)
  29. {
  30. BTIF_TRACE_DEBUG0("btif_task: hardware init failed");
  31. bte_main_disable();
  32. btif_queue_release();
  33. GKI_task_self_cleanup(BTIF_TASK);
  34. bte_main_shutdown();
  35. btif_dut_mode = 0;
  36. btif_core_state = BTIF_CORE_STATE_DISABLED;
  37. HAL_CBACK(bt_hal_cbacks,adapter_state_changed_cb,BT_STATE_OFF);
  38. break;
  39. }
  40. if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
  41. break;
  42. if(event & TASK_MBOX_1_EVT_MASK)
  43. {
  44. while((p_msg = GKI_read_mbox(BTU_BTIF_MBOX)) != NULL) //从消息队列中读取消息
  45. {
  46. BTIF_TRACE_VERBOSE1("btif task fetched event %x", p_msg->event);
  47. switch (p_msg->event)
  48. {
  49. case BT_EVT_CONTEXT_SWITCH_EVT:
  50. btif_context_switched(p_msg); //执行消息内部注册的回调
  51. break;
  52. default:
  53. BTIF_TRACE_ERROR1("unhandled btif event (%d)", p_msg->event & BT_EVT_MASK);
  54. break;
  55. }
  56. GKI_freebuf(p_msg);
  57. }
  58. }
  59. }
  60. btif_disassociate_evt();
  61. BTIF_TRACE_DEBUG0("btif task exiting");
  62. }
总结:btif_task实际上就是不停得从消息队列中取消息,并执行消息中注册的回调函数的过程

第二个核心进程时在bte_main_enable函数中创建的,代码如下:
  1. void bte_main_enable()
  2. {
  3. APPL_TRACE_DEBUG1("%s", __FUNCTION__);
  4. /* Initialize BTE control block */
  5. BTE_Init();//btu控制块的初始化
  6. lpm_enabled = FALSE;
  7. bte_hci_enable();//初始化厂商库并设置成power_on
  8. GKI_create_task((TASKPTR)btu_task, BTU_TASK, BTE_BTU_TASK_STR,
  9. (UINT16 *) ((UINT8 *)bte_btu_stack + BTE_BTU_STACK_SIZE),
  10. sizeof(bte_btu_stack));//创建第二大核心进程btu_task,主要是与L2CAP层的数据交互
  11. GKI_run(0);
  12. }
  1. BTU_API UINT32 btu_task (UINT32 param)
  2. {
  3. UINT16 event;
  4. BT_HDR *p_msg;
  5. UINT8 i;
  6. UINT16 mask;
  7. BOOLEAN handled;
  8. #if (defined(HCISU_H4_INCLUDED) && HCISU_H4_INCLUDED == TRUE)
  9. /* wait an event that HCISU is ready */
  10. BT_TRACE_0(TRACE_LAYER_BTU, TRACE_TYPE_API,
  11. "btu_task pending for preload complete event");
  12. for (;;)
  13. {
  14. event = GKI_wait (0xFFFF, 0); //
  15. if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
  16. {
  17. /* indicates BT ENABLE abort */
  18. BT_TRACE_0(TRACE_LAYER_BTU, TRACE_TYPE_WARNING,
  19. "btu_task start abort!");
  20. return (0);
  21. }
  22. else if (event & BT_EVT_PRELOAD_CMPL)
  23. {
  24. break;
  25. }
  26. else
  27. {
  28. BT_TRACE_1(TRACE_LAYER_BTU, TRACE_TYPE_WARNING,
  29. "btu_task ignore evt %04x while pending for preload complete",
  30. event);
  31. }
  32. }
  33. BT_TRACE_0(TRACE_LAYER_BTU, TRACE_TYPE_API,
  34. "btu_task received preload complete event");
  35. #endif
  36. /* Initialize the mandatory core stack control blocks
  37. (BTU, BTM, L2CAP, and SDP)
  38. */
  39. btu_init_core(); //初始化协议栈核心,btm l2cap 和sdp
  40. /* Initialize any optional stack components */
  41. BTE_InitStack(); //初始化协议栈profile 比如spp hid dun hfp RFCOMM等
  42. #if (defined(BTU_BTA_INCLUDED) && BTU_BTA_INCLUDED == TRUE)
  43. bta_sys_init(); //注册bta系统回调
  44. #endif
  45. /* Initialise platform trace levels at this point as BTE_InitStack() and bta_sys_init()
  46. * reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL
  47. */
  48. #if ( BT_USE_TRACES==TRUE )
  49. BTE_InitTraceLevels();
  50. #endif
  51. /* Send a startup evt message to BTIF_TASK to kickstart the init procedure */
  52. GKI_send_event(BTIF_TASK, BT_EVT_TRIGGER_STACK_INIT);
  53. //发送BT_EVT_TRIGGER_STACK_INIT事件给btif_task
  54. raise_priority_a2dp(TASK_HIGH_BTU);
  55. /* Wait for, and process, events */
  56. for (;;) //事件循环。。。
  57. {
  58. event = GKI_wait (0xFFFF, 0);
  59. //处理TASK_MBOX_0_EVT_MASK事件
  60. if (event & TASK_MBOX_0_EVT_MASK)
  61. {
  62. /* Process all messages in the queue */
  63. while ((p_msg = (BT_HDR *) GKI_read_mbox (BTU_HCI_RCV_MBOX)) != NULL)
  64. {
  65. /* Determine the input message type. */
  66. switch (p_msg->event & BT_EVT_MASK)
  67. {
  68. case BT_EVT_TO_BTU_HCI_ACL:
  69. /* All Acl Data goes to L2CAP */
  70. l2c_rcv_acl_data (p_msg);
  71. break;
  72. case BT_EVT_TO_BTU_L2C_SEG_XMIT:
  73. /* L2CAP segment transmit complete */
  74. l2c_link_segments_xmitted (p_msg);
  75. break;
  76. case BT_EVT_TO_BTU_HCI_SCO:
  77. #if BTM_SCO_INCLUDED == TRUE
  78. btm_route_sco_data (p_msg);
  79. break;
  80. #endif
  81. case BT_EVT_TO_BTU_HCI_EVT:
  82. btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
  83. GKI_freebuf(p_msg);
  84. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  85. /* If host receives events which it doesn't response to, */
  86. /* host should start idle timer to enter sleep mode. */
  87. btu_check_bt_sleep ();
  88. #endif
  89. break;
  90. case BT_EVT_TO_BTU_HCI_CMD:
  91. btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
  92. break;
  93. #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
  94. #if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
  95. case BT_EVT_TO_OBX_SR_MSG:
  96. obx_sr_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
  97. GKI_freebuf (p_msg);
  98. break;
  99. case BT_EVT_TO_OBX_SR_L2C_MSG:
  100. obx_sr_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
  101. GKI_freebuf (p_msg);
  102. break;
  103. #endif
  104. #if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
  105. case BT_EVT_TO_OBX_CL_MSG:
  106. obx_cl_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
  107. GKI_freebuf (p_msg);
  108. break;
  109. case BT_EVT_TO_OBX_CL_L2C_MSG:
  110. obx_cl_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
  111. GKI_freebuf (p_msg);
  112. break;
  113. #endif
  114. #if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
  115. case BT_EVT_TO_BIP_CMDS :
  116. bip_proc_btu_event(p_msg);
  117. GKI_freebuf (p_msg);
  118. break;
  119. #endif /* BIP */
  120. #if (BPP_SND_INCLUDED == TRUE || BPP_INCLUDED == TRUE)
  121. case BT_EVT_TO_BPP_PR_CMDS:
  122. bpp_pr_proc_event(p_msg);
  123. GKI_freebuf (p_msg);
  124. break;
  125. case BT_EVT_TO_BPP_SND_CMDS:
  126. bpp_snd_proc_event(p_msg);
  127. GKI_freebuf (p_msg);
  128. break;
  129. #endif /* BPP */
  130. #endif /* OBX */
  131. #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
  132. case BT_EVT_TO_BTU_SAP :
  133. sap_proc_btu_event(p_msg);
  134. GKI_freebuf (p_msg);
  135. break;
  136. #endif /* SAP */
  137. #if (defined(GAP_CONN_INCLUDED) && GAP_CONN_INCLUDED == TRUE && GAP_CONN_POST_EVT_INCLUDED == TRUE)
  138. case BT_EVT_TO_GAP_MSG :
  139. gap_proc_btu_event(p_msg);
  140. GKI_freebuf (p_msg);
  141. break;
  142. #endif
  143. case BT_EVT_TO_START_TIMER :
  144. /* Start free running 1 second timer for list management */
  145. GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
  146. GKI_freebuf (p_msg);
  147. break;
  148. case BT_EVT_TO_STOP_TIMER:
  149. if (btu_cb.timer_queue.p_first == NULL)
  150. {
  151. GKI_stop_timer(TIMER_0);
  152. }
  153. GKI_freebuf (p_msg);
  154. break;
  155. #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
  156. case BT_EVT_TO_START_QUICK_TIMER :
  157. GKI_start_timer (TIMER_2, QUICK_TIMER_TICKS, TRUE);
  158. GKI_freebuf (p_msg);
  159. break;
  160. #endif
  161. default:
  162. i = 0;
  163. mask = (UINT16) (p_msg->event & BT_EVT_MASK);
  164. handled = FALSE;
  165. for (; !handled && i < BTU_MAX_REG_EVENT; i++)
  166. {
  167. if (btu_cb.event_reg[i].event_cb == NULL)
  168. continue;
  169. if (mask == btu_cb.event_reg[i].event_range)
  170. {
  171. if (btu_cb.event_reg[i].event_cb)
  172. {
  173. btu_cb.event_reg[i].event_cb(p_msg);
  174. handled = TRUE;
  175. }
  176. }
  177. }
  178. if (handled == FALSE)
  179. GKI_freebuf (p_msg);
  180. break;
  181. }
  182. }
  183. }
  184. //处理TIMER_0_EVT_MASK事件
  185. if (event & TIMER_0_EVT_MASK)
  186. {
  187. TIMER_LIST_ENT *p_tle;
  188. GKI_update_timer_list (&btu_cb.timer_queue, 1);
  189. while ((btu_cb.timer_queue.p_first) && (!btu_cb.timer_queue.p_first->ticks))
  190. {
  191. p_tle = btu_cb.timer_queue.p_first;
  192. GKI_remove_from_timer_list (&btu_cb.timer_queue, p_tle);
  193. switch (p_tle->event)
  194. {
  195. case BTU_TTYPE_BTM_DEV_CTL:
  196. btm_dev_timeout(p_tle);
  197. break;
  198. case BTU_TTYPE_BTM_ACL:
  199. btm_acl_timeout(p_tle);
  200. break;
  201. case BTU_TTYPE_L2CAP_LINK:
  202. case BTU_TTYPE_L2CAP_CHNL:
  203. case BTU_TTYPE_L2CAP_HOLD:
  204. case BTU_TTYPE_L2CAP_INFO:
  205. case BTU_TTYPE_L2CAP_FCR_ACK:
  206. l2c_process_timeout (p_tle);
  207. break;
  208. case BTU_TTYPE_SDP:
  209. sdp_conn_timeout ((tCONN_CB *)p_tle->param);
  210. break;
  211. case BTU_TTYPE_BTM_RMT_NAME:
  212. btm_inq_rmt_name_failed();
  213. break;
  214. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  215. case BTU_TTYPE_RFCOMM_MFC:
  216. case BTU_TTYPE_RFCOMM_PORT:
  217. rfcomm_process_timeout (p_tle);
  218. break;
  219. #endif /* If defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE */
  220. #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
  221. case BTU_TTYPE_BNEP:
  222. bnep_process_timeout(p_tle);
  223. break;
  224. #endif
  225. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  226. case BTU_TTYPE_AVDT_CCB_RET:
  227. case BTU_TTYPE_AVDT_CCB_RSP:
  228. case BTU_TTYPE_AVDT_CCB_IDLE:
  229. case BTU_TTYPE_AVDT_SCB_TC:
  230. avdt_process_timeout(p_tle);
  231. break;
  232. #endif
  233. #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
  234. #if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
  235. case BTU_TTYPE_OBX_CLIENT_TO:
  236. obx_cl_timeout(p_tle);
  237. break;
  238. #endif
  239. #if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
  240. case BTU_TTYPE_OBX_SERVER_TO:
  241. obx_sr_timeout(p_tle);
  242. break;
  243. case BTU_TTYPE_OBX_SVR_SESS_TO:
  244. obx_sr_sess_timeout(p_tle);
  245. break;
  246. #endif
  247. #endif
  248. #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
  249. case BTU_TTYPE_SAP_TO:
  250. sap_process_timeout(p_tle);
  251. break;
  252. #endif
  253. case BTU_TTYPE_BTU_CMD_CMPL:
  254. btu_hcif_cmd_timeout((UINT8)(p_tle->event - BTU_TTYPE_BTU_CMD_CMPL));
  255. break;
  256. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  257. case BTU_TTYPE_HID_HOST_REPAGE_TO :
  258. hidh_proc_repage_timeout(p_tle);
  259. break;
  260. #endif
  261. #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
  262. case BTU_TTYPE_BLE_INQUIRY:
  263. case BTU_TTYPE_BLE_GAP_LIM_DISC:
  264. case BTU_TTYPE_BLE_RANDOM_ADDR:
  265. btm_ble_timeout(p_tle);
  266. break;
  267. case BTU_TTYPE_ATT_WAIT_FOR_RSP:
  268. gatt_rsp_timeout(p_tle);
  269. break;
  270. case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
  271. gatt_ind_ack_timeout(p_tle);
  272. break;
  273. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  274. case BTU_TTYPE_SMP_PAIRING_CMD:
  275. smp_rsp_timeout(p_tle);
  276. break;
  277. #endif
  278. #endif
  279. #if (MCA_INCLUDED == TRUE)
  280. case BTU_TTYPE_MCA_CCB_RSP:
  281. mca_process_timeout(p_tle);
  282. break;
  283. #endif
  284. case BTU_TTYPE_USER_FUNC:
  285. {
  286. tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
  287. (*p_uf)(p_tle);
  288. }
  289. break;
  290. default:
  291. i = 0;
  292. handled = FALSE;
  293. for (; !handled && i < BTU_MAX_REG_TIMER; i++)
  294. {
  295. if (btu_cb.timer_reg[i].timer_cb == NULL)
  296. continue;
  297. if (btu_cb.timer_reg[i].p_tle == p_tle)
  298. {
  299. btu_cb.timer_reg[i].timer_cb(p_tle);
  300. handled = TRUE;
  301. }
  302. }
  303. break;
  304. }
  305. }
  306. /* if timer list is empty stop periodic GKI timer */
  307. if (btu_cb.timer_queue.p_first == NULL)
  308. {
  309. GKI_stop_timer(TIMER_0);
  310. }
  311. }
  312. #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
  313. //处理TIMER_2_EVT_MASK事件
  314. if (event & TIMER_2_EVT_MASK)
  315. {
  316. btu_process_quick_timer_evt();
  317. }
  318. #endif
  319. #if (RPC_INCLUDED == TRUE)
  320. /* if RPC message queue event */
  321. //处理RPCGEN_MSG_EVT事件
  322. if (event & RPCGEN_MSG_EVT)
  323. {
  324. if ((p_msg = (BT_HDR *) GKI_read_mbox(RPCGEN_MSG_MBOX)) != NULL)
  325. RPCT_RpcgenMsg(p_msg); /* handle RPC message queue */
  326. }
  327. #endif
  328. #if (defined(BTU_BTA_INCLUDED) && BTU_BTA_INCLUDED == TRUE)
  329. //处理TASK_MBOX_2_EVT_MASK事件
  330. if (event & TASK_MBOX_2_EVT_MASK)
  331. {
  332. while ((p_msg = (BT_HDR *) GKI_read_mbox(TASK_MBOX_2)) != NULL)
  333. {
  334. bta_sys_event(p_msg);
  335. }
  336. }
  337. //处理TIMER_1_EVT_MASK事件
  338. if (event & TIMER_1_EVT_MASK)
  339. {
  340. bta_sys_timer_update();
  341. }
  342. #endif
  343. if (event & EVENT_MASK(APPL_EVT_7))
  344. break;
  345. }
  346. return(0);
  347. }
  348. 总结:btu_task等待并处理如下事件
  349. TASK_MBOX_0_EVT_MASK
  350. TIMER_0_EVT_MASK
  351. TIMER_2_EVT_MASK
  352. RPCGEN_MSG_EVT
  353. TASK_MBOX_2_EVT_MASK
  354. TIMER_1_EVT_MASK
  355. 处理事件的过程实际上就是调用回调函数的过程,具体过程请参考回调函数的注册!





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/820329
推荐阅读
相关标签
  

闽ICP备14008679号