Android Bluedroid source code analysis

Android Bluedroid source code analysis

 

https://i-blog.csdnimg.cn/blog_migrate/af39aa65e0c0be206bd92da6a9c8a9bc.png

1:应用层到协议层

 

Android的bt整体结构如图1所示:

  1. 应用层:使用蓝牙协议的各种应用,例如:蓝牙电话、音乐等。
  2. Framework层:主要是android系统向应用层开放的蓝牙的相关接口。
  3. Bluetooth Service:各种profile和adapter的服务端实现,framework端通过AIDL方式和这层进行调用和回调。
  4. Bluedroid:蓝牙协议栈的实现,Bluetooth Service层通过Jni方式与该层双向通信。

 

在bluedroid层,有一些专业术语如下:

 

BTIF: Bluetooth Interface 

BTU : Bluetooth Upper Layer 

BTM: Bluetooth Manager 

BTE: Bluetooth embedded system 

BTA :Blueetooth application layer 

CO: call out\CI: call in 

HF : Handsfree Profile 

HH: HID Host Profile 

HL: Health Device Profile

AV:audio\vidio 

AG: audio gateway

r: audio/video registration 

gattc: GATT client 

BLE: Bluetooth Low Energy

 

bluedroid协议栈中主要的分工框架子功能有如下分析:

整个bluedroid可以分为两大模块:BTA,BTE

BTIF:提供bluedroid对外的接口

BTA:bluedroid中各profile的逻辑实现和处理

BTE:bluedroid的内部处理,又细分为BTA,BTU,BTM和HCI

BTU:承接BTA与HCI

BTM:蓝牙配对与链路管理

HCI:读取或写入数据到蓝牙hw

 

https://i-blog.csdnimg.cn/blog_migrate/9d04e6cebea9d0ad562fedcac647e20d.png

图2:协议层

 

具体说下协议栈中主要工作线程有以下几个:(图2中是老的协议栈实现图解,主要使用了自实现的线程方式(osi/src/thread.cc);在android9.0中使用了新的自实现线程MessageLoopThread(./common/message_loop_thread.cc),其实是基于第三方chromium库实现; 这两种线程实现方式其实原理都是差不多的,都使用队列的方式处理事件。)

  1. main_thread("bt_main_thread"),该线程起到了承上启下的作用,
    1. 把HCI上来的事件或者数据处理好后向上传递给bta=>btif,再有bt_jni_thread向上传给java层代码处理。
    2. java层(java层线程)下发的命令和数据,首先通过btif接口,然后把btif=>bta下来的command、data发送给HCI层(放到队列里)交给bt_hci_thread。
  2. bt_startup_thread("bt_startup_thread"),
    1. 这个线程主要执行btu_task_start_up函数
      1. 初始化stack components(stack目录实现的各种组件)。
      2. 初始化bta系统。
      3. 启动bt_main_thread线程。
      4. 协议栈启动完成处于正常工作状态。
  3. management_thread("bt_stack_manager_thread")
    1. 协议栈启动过程,event_start_up_stack=>bte_main_enable=>BTU_StartUp=>到bt_startup_thread执行btu_task_start_up
    2.  
    3.  
  4. hci_thread("bt_hci_thread"),hci线程负责把上层的命令和数据发送到controller,也就是vendor提供的对接HCI层的库。
  5. jni_thread("bt_jni_thread"). 这个线程主要负责给java层反调用发送从HCI来的各种事件和命令结果,把其发送到bluetooth app(java代码)中处理。
  6. java层线程,
    1. 协议栈初始化过程,initNative(在android系统启动的时候应该已经初始化了协议栈,但是并没有启动,启动需要主动调用BluetoothAdapter的enable函数。)
    2. 协议栈启动过程,enableNative => enable(bt_interface_t接口) => stack_manager_get_interface()->start_up_stack_async() => 在线程bt_stack_manager_thread中执行event_start_up_stack。
  7. vendor的库线程,这个线程负责调用HCI层的回调,把数据传到HCI层之上。该线程会调用一系列的回调函数把数据发送到bt_hci_thread处理。
    1. 当前android最新使用HIDL,那么这种方式会有单独的binder线程监听并返回数据。
    2. 老版本使用vendor库,这种情况应该是vendor库里会有单独的线程之间回调hci库的回调函数。或者HCI层有单独的线程监听下层数据的到来。
  8. 12
  9. 12

 

蓝牙安全策略(在pairing之前需要建立acl连接):

The Bluetooth security model includes five distinct security features: pairing,

bonding, device authentication, encryption and message integrity.

• Pairing: the process for creating one or more shared secret keys

• Bonding: the act of storing the keys created during pairing for use in

subsequent connections in order to form a trusted device pair

• Device authentication: verification that the two devices have the same keys

• Encryption: message confidentiality

• Message integrity: protects against message forgeries

 

 

对于蓝牙层次HCI、L2CAP、RFCOMM三个层次分别用什么来区分通道呢:

  1. HCI层(在HOST和Controller之间)用handles区分识别不同的通道。其中,Connection Handles用于primary controller(除AMP外),另外两用用于AMP Controller。一旦一条Logical Link建立,primary controller会给Host分配一个Connection Handle。
    1. connections handles
    2. logical link handles
    3. physical link handles
  2. L2CAP使用CID区分不同的通道。CID由一个字标识,即两个bytes,其中0x0000不使用,0x0001--0x003f有固定的用途,具体可以去查相关描述的表格,在0x003f之后的可以动态分配,一般做为数据传输的cid
  3. RFCOMM使用channel区分不同的服务,其中Server Channel: 0是固定的,用于signaling。

 

 

 

Android brludroid协议栈当前在android源代码的/system/bt目录下,下面会分别对这个目录下的各个子目录逐一分析(本文基于Android P,可能和P之前的协议栈有一些异同,因为实现在不断的变化,但是本质其实是一样的)。

 

基于RFCOMM的service连接过程(参考SPP SPEC协议3.1.1):

  1. 首先建立ACL连接(HCI层通过handle句柄管理这些连接(包含ACL和SCO连接,每个连接完成后会有一个连接handle))。HCI共有四种类型的数据包:command(0x01)、acl(0x02)、sco(0x03)和event(0x04)
  2. 通过L2CAP协议的固定通道(0x0001 signaling)创建SDP的L2CAP connection(基于L2CAP之上,两个设备之间会相互创建对应的SDP connection)。
  3. 通过SDP的search request和attribute request获取对端设备相关服务的信息。获取rfcomm相关服务的Channel Number(Server channel)。例如获取Message Notification Server服务的Channel Number: 1(随机的分配或者固定都有可能)。获取Handsfree Audio Gateway的Channel Number: 8,这个channel number是rfcomm的server channel id,并非l2cap的channel ID(简称CID)。
  4. 创建基于L2CAP的rfcomm connection。
  5. 基于rfcomm并通过SDP获取的server channel id建立基于rfcomm协议的相关协议(例如上面得到的Message Notification Server服务(Channel Number: 1)和Handsfree Audio Gateway服务(Channel Number: 8))
  6. 从上面分析可以看出,l2cap和rfcomm都有channel number,但是他们有所区别。L2CAP传输是基于信道的概念,类似于fifo的通信通道,他是一个点对点的通道,每个通道都有一个独立的信道标识符(channel identifier,CID),以下简称CID。学习L2CAP,理解信道是非常重要的,在HCI层的配置信息交互完成之后,信道就会被建立,在ACL链路中通常会建立一个信令信道,如CID号为0x0001,用于发送控制指令。l2cap通过默认的固定通道(0x0001 signaling)建立新的协议连接(例如rfcomm,sdp,avdtp signaling等),同时自动分配channel id;rfcomm连接的建立则是基于在通过SDP读取各种服务的channel number。channel number(即server channel)已经是存在于对端设备的SDP的数据库中,服务在注册时已经分配了相应的server channel,透过SDP的各种请求对端设备获取相应服务的channel number后并基于channel number建立相应的服务通道。rfcomm之上的相关协议提供相应的服务,例如obex(GOEPv1.1基于rfcomm,GOEPv2.0直接基于l2cap),hfp,mns,avrcp等。

 

  1. Audio_a2dp_hw目录,该目录主要实现了音频模块针对ad2p的硬件抽象层的实现,供音频模块来控制a2dp的通路切换,声音和音量的控制。生成动态库audio.a2dp.default.so
    1. Audio_hw_a2dp.cc,主要实现了HAL层模块AUDIO_HARDWARE_MODULE_ID,"A2DP Audio HW HAL",模拟a2dp为音频驱动的一个模块。输出HAL层模块:一个模拟音频设备。
  2. audio_bluetooth_hw目录,Add generic audio HW module for Bluetooth audio HAL V. This is loaded from audio HAL when initials the audio HW module, bluetooth_audio, and uses Bluetooth audio HAL V2 to provide stream APIs for control and data path. When the audio framework opens different input or output streams, it uses the audio device type to choose which SessionType is and pass to Bluetooth audio HAL so associates with the Provider / Port pair and communicate with the Bluetooth stack.

* Audio contrl path uses IBluetoothAudioPort interface to interact with the Bluetooth stack.

* Audio data path uses HIDL Fast Message Queue that is maintained within IBluetoothAudioProvider HIDL and is ready after session started.

生成动态库audio.bluetooth.default.so

    1. audio_bluetooth_hw.cc,输出HAL层模块:默认的Audio HAL V2的模拟音频设备
  1. audio_hal_interface目录,Add stack side interface for Bluetooth audio HAL V2,This bases on the new Bluetooth Audio HAL V2 to provide a HIDL based

interface for the stack. There will be a common interface named

BluetoothAudioClientInterface that the stack can register its session for

stream of A2DP or Hearing Aid. When the stack registers to this

pre-implemented BluetoothAudioClientInterface, the audio HAL can control

the stream state and the stack can report results to the audio HAL.

When running for software encoding, there will also data path via FMQ to

provide the bridge between audio HAL and the stack. This change contains

A2DP software encoding (legacy) only.。生成静态库libbt-audio-hal-interface

    1. client_interface.cc,实现Audio HAL V2客户端代码。
    2. a2dp_encoding.cc,实现a2dp端HIDL客户端,可以控制a2dp的HAL层对端(server端)流状态,和下发统计数据到HAL层对端(server端)。
    3. hearing_aid_software_encoding.cc,同a2dp类似的hearing aid(助听器)的实现。
    4. 12
  1. audio_hearing_aid_hw目录,该目录主要实现了音频模块针对hearing aid(助听器)的硬件抽象层的实现,供音频模块来控制hearingaid的通路切换,声音和音量的控制。生成动态库audio.hearing_aid.default.so
    1. audio_hearing_aid_hw.cc,该文件同audio_hw_a2dp.cc功能相同。输出HAL层模块:一个模拟音频设备。
  2. binder目录, Bluetooth: move AIDL files related to Bluetooth into system/bt (2/3),moves *.aidl files from frameworks/base/core/java/android/bluetooth into system/bt/binder. This is in preparation to convert the Bluetooth deamon into native  implementation piece by piece. In order to do that, one must have C++ header files, and paths to them with AIDL files, and */java/* folder didn't seem as proper place for that. Additionally, keeping AIDL files out of framework/base will not require creating dependency on this huge project, which should help keeping the compilation fast. 这些AIDL是framework和bluetooth app间的通信接口。这些aidl的native实现在、bt/service/ipc/binder/中,全部的aidl逐步转化为native实现(实际上又从native转为了aidl(bt/service/common/android/bluetooth/))。这个目录基本上没有什么内容了。生成静态库libbluetooth-binder
  3. device目录,这个目录主要实现了控制器端参数的读取和设置功能。主要调用了hci_packet_parser.cc和packet_fragementer.cc里实现的相关函数,在蓝牙设备启动和reset中会初始化这些参数。生成静态库libbtdevice
    1. controller.cc,这个文件主要实现控制器参数的读取和设置。通过给控制器发送HCI命令获取控制器支持的参数。 输出模块CONTROLLER_MODULE
    2. interop.cc,各种workaround对于各种蓝牙设备的各种操作,为了适配所有的蓝牙设备。输出模块INTEROP_MODULE
    3. 21
    4. 12
  4. conf目录,该目录主要是bt协议栈的配置文件。生成配置文件
    1. bt_did.conf,对协议栈的vendorId,Product ID & Product Version等配置。
    2. bt_stack.conf,蓝牙日志等的一些配置。
  5. utils目录,主要实现一些用到的util函数。
    1. bt_utils.cc,主要实现一个函数raise_priority_a2dp,Raise task priority for A2DP streaming。输出模块BT_UTILS_MODULE
    2. 12
  6. bta目录,这个是蓝牙协议栈的bta(bluetooth application)层
    1. ag目录
      1. bta_ag_act.cc, This file contains action functions for the audio gateway.
      2. bta_ag_api.cc
      3. bta_ag_at.cc, BTA AG AT command interpreter.
      4. bta_ag_at.h
      5. bta_ag_cfg.cc, This file contains compile-time configurable constants for the audio gateway.
      6. bta_ag_cmd.cc
      7. bta_ag_int.h
      8. bta_ag_main.cc, This is the main implementation file for the BTA audio gateway.
      9. bta_ag_rfc.cc, This file contains the audio gateway functions controlling the RFCOMM connections
      10. bta_ag_sco.cc, This file contains functions for managing the SCO connection used in AG.
      11. bta_ag_sdp.cc, This file contains the audio gateway functions performing SDP operations.
      12. 1
      13. 21
      14. 2
    2. ar目录
      1. bta_ar.cc, This is the implementation for the audio/video registration module.
    3. av目录
      1. bta_av_aact.cc, This file contains action functions for advanced audio/video stream state machine. these functions are shared by both audio and video streams.
      2. bta_av_act.cc, This file contains action functions for advanced audio/video main state machine
      3. bta_av_api.cc, This is the implementation of the API for the advanced audio/video (AV) subsystem of BTA, Broadcom's Bluetooth application layer for mobile phones
      4. bta_av_cfg.cc, This file contains compile-time configurable constants for advanced audio/video
      5. bta_av_ci.cc, This is the implementation file for advanced audio/video call-in functions
      6. bta_av_main.cc, This is the main implementation file for the BTA advanced audio/video.
      7. bta_av_ssm.cc, This is the stream state machine for the BTA advanced audio/video.
    4. dm目录
      1. bta_dm_act.cc, This file contains the action functions for device manager state machine
      2. bta_dm_api.cc, This is the API implementation file for the BTA device manager.
      3. bta_dm_cfg.cc, This file contains compile-time configurable constants for the device manager
      4. bta_dm_ci.cc, This is the implementation file for device mananger call-in functions
      5. bta_dm_main.cc, This is the main implementation file for the BTA device manager.
      6. bta_dm_pm.cc, BLE power manager.
    5. gatt目录
      1. bta_gattc_act.cc, This file contains the GATT client action functions for the state machine
      2. bta_gattc_api.cc, This is the implementation of the API for GATT module of BTA.
      3. bta_gattc_cache.cc, This file contains the GATT client discovery procedures and cache related functions.
      4. bta_gattc_main.cc, This file contains the GATT client main functions and state machine.
      5. bta_gattc_queue.cc, 读写GATT交互数据
      6. bta_gattc_utils.cc, This file contains the GATT client utility function.
      7. bta_gatts_act.cc, This file contains the GATT Server action functions for the state machine
      8. bta_gatts_api.cc, This is the implementation of the API for GATT server of BTA.
      9. bta_gatts_main.cc, This file contains the GATT server main functions and state machine.
      10. bta_gatts_utils.cc, This file contains the GATT client utility function.
      11. database.cc, GATT数据库处理函数。
      12. database_builder.cc, 数据库builder。
      13.  
    6. hd目录
      1. bta_hd_act.cc, This file contains the HID device action functions.
      2. bta_hd_api.cc, This file contains the HID DEVICE API in the subsystem of BTA.
      3. bta_hd_main.cc, This file contains the HID host main functions and state machine.
    7. hearing_aid目录
      1. hearing_aid.cc, Hearing AID profile,助听设备协议实现
      2. hearing_aid_audio_source.cc,
    8. hf_client目录
      1. bta_hf_client_act.cc, This file contains action functions for the handsfree client.
      2. bta_hf_client_api.cc, This is the implementation of the API for the handsfree (HF role) subsystem of BTA
      3. bta_hf_client_at.cc, AT命令相关函数。
      4. bta_hf_client_main.cc, hfp client主要函数实现。
      5. bta_hf_client_rfc.cc, This file contains the audio gateway functions controlling the RFCOMM connections
      6. bta_hf_client_sco.cc, sco连接请求与关闭等函数。
      7. bta_hf_client_sdp.cc,This file contains the audio gateway functions performing SDP operations
      8. 12
    9. hh目录
      1. bta_hh_act.cc, This file contains the HID host action functions.
      2. bta_hh_api.cc, This file contains the HID HOST API in the subsystem of BTA.
      3. bta_hh_cfg.cc, This file contains compile-time configurable constants for the BTA Hid Host
      4. bta_hh_le.cc, LE HID related functionality.
      5. bta_hh_main.cc, This file contains the HID host main functions and state machine.
      6. bta_hh_utils.cc,
    10. hl目录
      1. bta_hl_main.cc, This file contains the HeaLth device profile main functions and state machine.
    11. jv目录
      1. bta_jv_act.cc, This file contains action functions for BTA JV(Java Interface) APIs.
      2. bta_jv_api.cc, This is the implementation of the JAVA API for Bluetooth Wireless Technology (JABWT) as specified by the JSR82 specificiation
      3. bta_jv_cfg.cc, This file contains compile-time configurable constants for advanced audio
    12. mce目录
      1. bta_mce_act.cc, This file contains action functions for MCE.
      2. bta_mce_api.cc, This is the implementation of the API for MCE(Message Access Profile (MCE role)) subsystem.
      3. bta_mce_cfg.cc, This file contains compile-time configurable constants for MCE
      4. bta_mce_main.cc, This is the main implementation file for the BTA MCE I/F
      5. 12
    13. pan目录
      1. bta_pan_act.cc, This file contains the pan action functions for the state machine.
      2. bta_pan_api.cc, This is the implementation of the API for PAN subsystem of BTA, Broadcom's Bluetooth application layer for mobile phones.
      3. bta_pan_ci.cc, This is the implementation file for data gateway call-in functions.
      4. bta_pan_main.cc, This file contains the PAN main functions and state machine.
    14. pb目录
      1. bta_pbs_int.h, This is the private file for the phone book access server (PBS).
    15. sdp 目录
      1. bta_sdp.cc, This is the main implementation file for the BTA SDP I/F.
      2. bta_sdp_act.cc, This file contains action functions for SDP search.
      3. bta_sdp_api.cc, This is the implementation of the API for SDP search subsystem
      4. bta_sdp_cfg.cc, This file contains compile-time configurable constants for SDP Search
      5. 12
    16. sys目录
      1. bta_sys_conn.cc, Routes connection status callbacks from various sub systems to DM
      2. bta_sys_main.cc, Start a protocol timer for the specified amount
    17. 12
    18. 12
  7. btcore目录
    1. src目录
      1. device_class.cc, Provides Class Of Device primitive as specified in the bluetooth spec. [Class Of Device] (https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband)
      2. hal_util.cc, 实现函数hal_util_load_bt_library,Load the Bluetooth shared library module.
      3. module.cc, 蓝牙各个module(蓝牙协议栈的各个模块)的处理函数。
      4. osi_module.cc, 实现OSI_MODULE。
      5. property.cc, Bluetooth Adapter and Remote Device property types.
  8. btif目录
    1. avrcp子目录,实现AVRCP Target Service
      1. avrcp_service.cc,实现AVRCP Target Service,通过bt_interface_t bluetoothInterface向上提供AVRCP Target service接口。
      2. 12
    2. co子目录
      1. bta_av_co.cc, This is the advanced audio/video call-out function implementation for BTIF.
      2. bta_dm_co.cc, callout function implemention for Device Management(DM)
      3. bta_gatts_co.cc, callout function for gatt service.
      4. bta_hh_co.cc, callout function for HID host.
      5. bta_pan_co.cc, callout function for PAN.
      6. 12
      7. 12
    3. src子目录
      1. bluetooth.cc, 提供给上层的接口(为JNI之上的bluetooth.apk提供profile接口和其他蓝牙功能接口)。
      2. btif_a2dp.cc,
      3. btif_a2dp_audio_interface.cc,
      4. btif_a2dp_audio_interface_linux.cc,
      5. btif_a2dp_control.cc,
      6. btif_a2dp_sink.cc,解码音频数据播放等。
      7. btif_a2dp_source.cc, a2dp相关接口。
      8. btif_av.cc, a2dp向上层提供的a2dp协议接口。
      9. btif_avrcp_audio_track.cc, 为avrcp创建audio_track,播放音乐的实例对象。
      10. btif_avrcp_audio_track_linux.cc
      11. btif_ble_advertiser.cc, BLE广播相关实现
      12. btif_ble_scanner.cc, BLE扫描相关实现
      13. btif_bqr.cc, Bluetooth Quality Report, 蓝牙信号和数据传输的质量报告。
      14. btif_config.cc, bt_config.conf配置文件解析。曾经连接过的bonded设备相关信息。
      15. btif_config_transcode.cc, 配置文件相关解析函数
      16. btif_core.cc,实现bt_jni_thread(此线程调用app层(bluetooth.apk)回调方法)
      17. btif_debug.cc, bt_snoop debug。
      18. btif_debug_btsnoop.cc, bt_snoop debug。
      19. btif_debug_conn.cc, debug连接断开原因。
      20. btif_dm.cc, 设备管理相关功能。(各种profile 服务的启动和关闭,设备名,绑定状态,设备黑名单等等)
      21. btif_gatt.cc, 实现GATT相关接口。
      22. btif_gatt_client.cc, GATT客户端接口。
      23. btif_gatt_server.cc, GATT服务端接口。
      24. btif_gatt_util.cc, gatt常用函数实现。
      25. btif_hd.cc, HID Device Profile Bluetooth Interface
      26. btif_hearing_aid.cc, Hearing Aid Profile Interface
      27. btif_hf.cc, Hands free profile interface.
      28. btif_hf_client.cc, Hands free client profile interface.
      29. btif_hh.cc, HID Host Profile Bluetooth Interface
      30. btif_hl.cc, Health Device Profile Bluetooth Interface
      31. btif_mce.cc, Message Access Profile (MCE role) Bluetooth Interface
      32. btif_pan.cc, PAN Profile Bluetooth Interface
      33. btif_profile_queue.cc, Bluetooth remote device connection queuing implementation.
      34. btif_rc.cc, Bluetooth AVRCP implementation
      35. btif_sdp.cc, SDP Bluetooth Interface. Implements the generic message handling and search functionality.
      36. btif_sdp_server.cc, SDP server Bluetooth Interface to create and remove SDP records
      37. btif_sock.cc, socket相关接口,处理SCO,L2CAP和RFCOMM的监听与连接建立。
      38. btif_sock_l2cap.cc, 实现L2CAP的socket函数。
      39. btif_sock_rfc.cc, 实现RFCOMM的socket函数。
      40. btif_sock_sco.cc, 实现SCO的socket函数。
      41. btif_sock_sdp.cc, This module provides an abstraction on top of the lower-level SDP database code for registration and discovery of various bluetooth sockets.
      42. btif_sock_thread.cc, socket select thread.
      43. btif_sock_util.cc, socket utils.
      44. btif_storage.cc, Stores the local BT adapter and remote device properties in NVRAM storage, typically as xml file in the mobile's filesystem.
      45. btif_uid.cc, Contains data structures and functions for keeping track of socket usage per app UID. 记录应用蓝牙通信数据大小
      46. btif_util.cc, btif utils.
      47. stack_manager.cc, 蓝牙协议栈start和shutdown管理类,负责一些资源的创建与释放,并且启动bt_jni_thread线程。
      48.  
      49. 12
      50. 12
      51. 12
    4. 2
    5. 2
  9. common目录
    1. benchmark目录
      1. thread_performance_benchmark.cc, 定义了一些蓝牙结构体和参数及接口
      2. timer_performance_benchmark.cc, Timer functions.
    2. 12
  10. device目录
    1. include目录
    2. src目录
      1. controller.cc, controller_module implementation.
      2. esco_parameters.cc, esco_parameters.
      3. interop.cc, 设备交互的一些参数。(连接方式,配对方式,Pin请求等等配置参数设置)
    3. 12
    4. 12
    5. 12
  11. embdrv目录
    1. g722目录,此目录实现g722音频编解码器。
    2. sbc目录,此目录实现sbc音频编解码器。
    3. 12
    4. 12
  12. gd目录
    1. 12
    2. 12
  13. hci目录,该目录主要实现了蓝牙HCI层的各种功能。命令,事件,数据的上传和下发,报文的组装和分拆等。生成静态库libbt-hci
    1. btsnoop.cc,btsnoop的log日志的相关函数。输出模块BTSNOOP_MODULE
    2. btsnoop_mem.cc,btsnoop的log日志内存中保存相关函数。
    3. btsnoop_net.cc,btsnoop的log日志通过网络socket进行保存的相关函数。可以通过socket发送到远端设备。
    4. hci_inject.cc,通过网络socket方式,把hci命令或者数据(acl和sco)发送到控制器。
    5. hci_layer.cc,hci层数据上传和下发的函数实现。输出模块HCI_MODULE
    6. hci_layer_android.cc,hci层HIDL客户端接口,调用hci_layer.cc实现的各种函数,实现通过HIDL与server端(蓝牙固件端)进行注册控制器端数据上传的回调函数和下发数据到控制器端。HCI层包含四种数据:命令,事件,acl数据和sco数据。
    7. hci_packet_factory.cc,实现组装各种hci命令,用于下发到控制器端,读取控制器的各种参数。
    8. hci_packet_parser.cc,对hci报文的解析函数。用于解析控制器返回的各种配置参数。和hci_packe_factory.cc是相对应的函数。这两个文件是为了在蓝牙协议栈启动时获取控制器的各种参数支持,或者设置参数给控制器(make_set_event_mask)。
    9. packet_fragementer.cc,对HCI上传来的报文进行重新组装并上报给上层,对HCI下发的数据和命令进行分包并下发给控制器。(由于下发的ACL数据包大小在controller端有最大值的限制,所以需要在下发和上传时需要拆分或者组装,使用HCI_Read_Buffer_Size命令来获取controller的buff大小)
    10. 12
    11. 12
  14. Include目录
    1. hardware目录,HAL层接口实现。向HAL层提供回调函数。
    2. 12
    3. 1212
    4. 12
  15. internal_include
    1. bte.h,this file contains constants and definitions for the bte project
    2. bte_appl.h, This is the interface file for the bte application task.
    3. 12
  16. main目录,bte实现(包含bta,btu,btm,hci),也就是说bte是bluedroid的核心实现(基本包含了所有内容,除了btif),在其上是btif(对上层的接口)。输出核心动态库libbluetooth.so
    1. bte_config.cc,分析和加载bt_did.conf配置文件。Parses the specified Device ID configuration file and registers the Device ID records with SDP.(UUID_SERVCLASS_PNP_INFORMATION
    2. stack_config.cc,分析和加载bt_stack.conf配置文件(测试和log配置等)。
    3. bte_in。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。it.cc,实现函数BTE_InitStack,Initialize the optional stack components。初始化一些optional的stack组件,必现在初始化core的stack之后执行,这些stack组件在stack目录中实现。
    4. bte_init_cpp_logging.cc,Initiates the logging for C++,初始化bt_statck.conf中的log相关设置。
    5. bte_logmsg.cc,读取bt_statck.conf中log级别并配置,输出模块BTE_LOGMSG_MODULE
    6. bte_main.cc,该文件起到了承上启下的作用,提供接口把L2CAP数据下发到HCI层和设置回调把HCI数据上传到上层。
      1. 主要实现了一些模块初始化和销毁函数,
      2. post_to_main_message_loop,该函数负责把HCI层上来的数据抛到bt_main_thread的队列中(包含HCI层上来的所有的数据(除去在HCI层过滤掉的),包含Event和ACL包或者SCO包),出队时由btu_hci_msg_process函数处理该数据。
      3. bte_main_enable, BTE MAIN API - Creates all the BTE tasks. Should be called part of the Bluetooth stack enable sequence, 启动BTSNOOP_MODULE和HCI_MODULE,启动BTU。
      4. bte_main_boot_entry,BTE MAIN API - Entry point for BTE chip/stack initialization,并设置HCI层的向上传数据的回调函数post_to_main_message_loop。
      5. bte_main_hci_send,BTE MAIN API - This function is called by the upper stack to send an HCI message. The function displays a protocol trace message (if enabled), and then calls the 'transmit' function associated with the currently selected HCI transport
    7. 21
  17. osi目录,该目录主要包含了许多蓝牙协议栈自定义的数据结构,例如:alloctor(内存分配工具)、hashmap、list、array、fixed_queue、config、buffer、socket、thread、alarm、wacklock、samphore等,比较重要的有以下几个文件。生成静态库libosi
    1. alarm.cc,这个文件主要使用一个单独的线程,实现了计时器功能,在未来某一个时间点执行一个具体的操作。
    2. allocator.cc,自定义内存分配器。
    3. compat.cc,Compatibility functions for non-bionic C libraries。
    4. config.cc,蓝牙协议栈配置文件相关操作函数。
    5. fixed_queue.cc,自定义队列相关操作函数,与自定义thread和reactor相互联系,在有消息入队和出队时自动执行相关函数,附着到一个thread的reactor中。
    6. future.cc,该文件实现一个线程间通信的锁机制,等待事件完成并传递事件执行结果。
    7. properties.cc,android系统属性读写函数。
    8. reactor.cc,线程主运行函数,run_reactor函数会进入死循环并进入epoll_wait等待新的事件产生并处理,可以监听多个fd,注册多个queue。
    9. ringbuffer.cc,环形队列数据结构。
    10. semaphore.cc,自实现semaphore信号量机制,用于在reactor中注册fd,并监听事件。
    11. socket.cc,给予标准linux local socket自实现进程或者线程间数据的传递,注册到reactor中并监听。
    12. socket_utils目录下实现了android和标准linux的local socket的一些bind,listen和connect函数,没有与reactor联合使用。
    13. thread.cc,这个文件主要实现了自定义线程类函数,与reactor和queue等,联合使用,监听各种fd并自动回调处理注册的各种回调函数。在协议栈中当前有下面几个用到该线程类("alarm_default_callbacks"、"alarm_dispatcher"、"btif_sock")。
    14. wakelock.cc,这个是android系统的cpu锁机制,获取锁后可以使得cpu不休眠。
    15. 12
  18. packet目录
    1. avrcp目录,avrcp相关报文的组装与解析。
    2. base目录,基本的packet结构(用于avrcp packet)。
    3. 12
    4. 12
  19. profile目录
    1. avrcp目录
      1. device.cc, A class representing a connection with a remote AVRCP device. It holds all the state and message handling for the device that it represents.
    2. sdp目录
      1. common目录
        1. data_element_reader.cc, A helper class that helps extract data element objects from SDP packets.
    3. 12
  20. proto目录
    1. bluetooth proto buffer实现
    2. 12
    3. 12
  21. service目录(提供蓝牙各种服务的实现,并提供命令行客户端控制蓝牙协议栈),个人分析认为是用于协议栈测试用。
    1. client目录
      1. main.cc,提供命令行接口控制蓝牙各种服务。
    2. common目录,提供一般的函数功能实现
    3. 12
    4. 12
    5. 12
    6. 12
  22. stack目录,stack各个组件的实现。
    1. a2dp目录
      1. a2dp_aac.cc,Utility functions to help build and parse the AAC Codec Information Element and Media Payload.
      2. a2dp_aac_decoder.cc, aac decoder
      3. a2dp_aac_encoder.cc,aac encoder
      4. a2dp_api.cc,Common API for the Advanced Audio Distribution Profile (A2DP)
      5. a2dp_codec_config.cc,A2DP Codecs Configuration
      6. a2dp_int.h,
      7. a2dp_sbc.cc,Utility functions to help build and parse SBC Codec Information Element and Media Payload.
      8. a2dp_sbc_decoder.cc,SBC decoder
      9. a2dp_sbc_encoder.cc,SBC encoder
      10. a2dp_sbc_up_sample.cc,This module contains utility functions for dealing with SBC data frames and codec capabilities.
      11. a2dp_vendor.cc,Vendor Specific A2DP Codecs Support
      12. a2dp_vendor_aptx.cc,Utility functions to help build and parse the aptX Codec Information Element and Media Payload.
      13. a2dp_vendor_aptx_encoder.cc,aptx encoder
      14. a2dp_vendor_aptx_hd.cc,Utility functions to help build and parse the aptX-HD Codec Information Element and Media Payload.
      15. a2dp_vendor_aptx_hd_encoder.cc,Encoder for aptX-HD Source Codec
      16. a2dp_vendor_ldac.cc,Utility functions to help build and parse the LDAC Codec Information Element and Media Payload.
      17. a2dp_vendor_ldac_abr.cc,LDAC ABR(Adaptive Bit Rate) Source Code
      18. a2dp_vendor_ldac_decoder.cc,Decoder for LDAC Source Codec
      19. a2dp_vendor_ldac_encoder.cc,Encoder for LDAC Source Codec
      20. 12
      21. 12
      22. 12
    2. avct目录,
      1. avct_api.cc,This module contains API of the audio/video control transport protocol.
      2. avct_bcb_act.cc,This module contains action functions of the browsing control state machine.
      3. avct_ccb.cc,This module contains functions which operate on the AVCTP connection control block.
      4. avct_l2c.cc,This AVCTP module interfaces to L2CAP
      5. avct_l2c_br.cc,This AVCTP module interfaces to L2CAP
      6. avct_lcb.cc,This module contains the link control state machine and functions which operate on the link control block.
      7. avct_lcb_act.cc,This module contains action functions of the link control state machine.
    3. avdt目录
      1. avdt_ad.cc,This module contains the AVDTP adaption layer.
      2. avdt_api.cc,This module contains API of the audio/video distribution transport protocol.
      3. avdt_ccb.cc,This module contains the channel control block state machine and functions which operate on the channel control block.
      4. avdt_ccb_act.cc,This module contains the action functions associated with the channel control block state machine.
      5. avdt_defs.h,This contains constants definitions and other information from the AVDTP specification.  This file is intended for use internal to AVDT only.
      6. avdt_int.h,This file contains interfaces which are internal to AVDTP.
      7. avdt_l2c.cc,This AVDTP adaption layer module interfaces to L2CAP.
      8. avdt_msg.cc,This module contains functions for parsing and building AVDTP signaling messages.  It also contains functions called by the SCB or CCB state machines for sending command, response, and reject messages.  It also contains a function that processes incoming messages and dispatches them to the appropriate SCB or CCB.
      9. avdt_scb.cc,This module contains the stream control block and functions which operate on the stream control block.
      10. avdt_scb_act.cc,This module contains the action functions associated with the stream control block state machine.
      11. 12
    4. avrc目录
      1. avrc_api.cc,Interface to AVRCP mandatory commands
      2. avrc_bld_ct.cc,Global data
      3. avrc_bld_tg.cc,Global data
      4. avrc_int.h,AVRCP internal header file.
      5. avrc_opt.cc,Interface to AVRCP optional commands
      6. avrc_pars_ct.cc,Global data
      7. avrc_pars_tg.cc,Global data
      8. avrc_sdp.cc,AVRCP SDP related functions
      9. avrc_utils.cc,avrcp utils
      10. 12
    5. bnep目录
      1. bnep_api.cc,This file contains the BNEP API code
      2. bnep_int.h,This file contains internally used BNEP definitions
      3. bnep_main.cc,G L O B A L    B N E P       D A T A
      4. bnep_utils.cc,This file contains BNEP utility functions
      5. 21
    6. btm目录
      1. ble_advertiser_hci_interface.cc,This class is an abstraction of HCI commands used for managing advertisements. Please see VSC HCI SPEC at https://static.googleusercontent.com/media/source.android.com/en//devices/Android-6.0-Bluetooth-HCI-Reqs.pdf and Bluetooth 5.0 "Advertising Extension" feature for more details
      2. btm_acl.cc,This file contains functions that handle ACL connections. This includes operations such as hold and sniff modes, supported packet types. This module contains both internal and external (API) functions. External (API) functions are distinguishable by their names beginning with uppercase BTM.
      3. btm_ble.cc,This file contains functions for BLE device control utilities, and LE security functions.
      4. btm_ble_addr.cc,This file contains functions for BLE address management.
      5. btm_ble_adv_filter.cc,BLE advertiser filter.
      6. btm_ble_batchscan.cc,batch scan.
      7. btm_ble_bgconn.cc, This file contains functions for BLE whitelist operation.
      8. btm_ble_bgconn.h
      9. btm_ble_connection_establishment.cc, ble连接创建状态处理函数。
      10. btm_ble_cont_energy.cc,energy info回调。
      11. btm_ble_gap.cc,This file contains functions for BLE GAP.
      12. btm_ble_int.h,this file contains the main Bluetooth Manager (BTM) internal definitions
      13. btm_ble_int_types.h
      14. btm_ble_multi_adv.cc,BleAdvertisingManager implementation.
      15. btm_ble_privacy.cc, This file contains functions for BLE controller based privacy.
      16. btm_dev.cc, This file contains functions for the Bluetooth Device Manager
      17. btm_devctl.cc, This file contains functions that handle BTM interface functions for the Bluetooth device including Rest, HCI buffer size and others
      18. btm_inq.cc, This file contains functions that handle inquiries. These include setting discoverable mode, controlling the mode of the Baseband, and maintaining a small database of inquiry responses, with API for people to browse it.
      19. btm_int.h, this file contains the main Bluetooth Manager (BTM) internal definitions.
      20. btm_int_types.h
      21. btm_main.cc, This file contains the definition of the btm control block.
      22. btm_pm.cc, This file contains functions that manages ACL link modes.  This includes operations such as active, hold, park and sniff modes. This module contains both internal and external (API) functions. External (API) functions are distinguishable by their names beginning with uppercase BTM.
      23. btm_sco.cc, This file contains functions that handle SCO connections. This includes operations such as connect, disconnect, change supported packet types.
      24. btm_sec.cc, This file contains functions for the Bluetooth Security Manager
      25. 12
    7. btu目录
      1. btu_hcif.cc,This file contains functions that interface with the HCI transport. On the receive side, it routes events to the appropriate handler, e.g. L2CAP, ScoMgr. On the transmit side, it manages the command transmission.
      2. btu_init.cc,btu init functions.
      3. btu_task.cc,create bt_main_thread,
      4. 12
    8. crypto_toolbox目录,加解密工具。
    9. gap目录
      1. gap_ble.cc,BLE GAP functions
      2. gap_conn.cc,GAP connections functions
    10. gatt目录
      1. att_protocol.cc,this file contains ATT protocol functions.
      2. connection_manager.cc,
      3. connection_manager.h,connection_manager takes care of all the low-level details of LE connection initiation. It accept requests from multiple subsystems to connect to devices, and multiplex them into whitelist add/remove, and scan parameter changes.
      4. gatt_api.cc,this file contains GATT interface functions
      5. gatt_attr.cc,this file contains the main GATT server attributes access request handling functions.
      6. gatt_auth.cc,this file contains GATT authentication handling functions
      7. gatt_cl.cc,this file contains the main GATT client functions
      8. gatt_db.cc,this file contains GATT database building and query functions
      9. gatt_int.h,
      10. gatt_main.cc,this file contains the main ATT functions
      11. gatt_sr.cc,this file contains the GATT server functions
      12. gatt_utils.cc,this file contains GATT utility functions
      13. 12
    11. hcic目录
      1. hciblecmds.cc,This file contains function of the HCIC unit to format and send BLE HCI commands.
      2. hcicmds.cc,This file contains function of the HCIC unit to format and send HCI commands
    12. hid目录
      1. hid_conn.h,This file contains HID connection internal definitions
      2. hidd_api.cc,This file contains the HID Device API entry points
      3. hidd_conn.cc,this file contains the connection interface functions
      4. hidd_int.h,This file contains HID DEVICE internal definitions
      5. hidh_api.cc,This file contains the HID HOST API entry points
      6. hidh_conn.cc,this file contains the connection interface functions
      7. hidh_int.h,This file contains HID HOST internal definitions
    13. include目录,头文件
    14. l2cap目录
      1. l2cap_client.cc,L2CAP client functions。
      2. l2c_api.cc,This file contains the L2CAP API code
      3. l2c_ble.cc,this file contains functions relating to BLE management.
      4. l2c_csm.cc,This file contains the L2CAP channel state machine
      5. l2c_fcr.cc,This file contains the L2CAP 1.2 Flow Control and retransmissions functions
      6. l2c_int.h,This file contains L2CAP internal definitions
      7. l2c_link.cc,this file contains the functions relating to link management. A "link" is a connection between this device and another device. Only ACL links are managed.
      8. l2c_main.cc,This file contains the main L2CAP entry points
      9. l2c_utils.cc,This file contains L2CAP utility functionsd
    15. mcap目录
      1. mca_cact.cc,This is the implementation file for the MCAP Control Channel Action Functions(Multi-Channel Adaptation Protocol
    16. pan目录
      1. pan_api.cc,This file contains main functions to support PAN profile commands and events.
      2. pan_int.h,This file contains internally used PAN definitions
      3. pan_main.cc,This file contains main functions to support PAN profile commands and events.
      4. pan_utils.cc,This file contains main functions to support PAN profile commands and events.
    17. rfcomm目录
      1. port_api.cc,this file contains the Serial Port API code
      2. port_int.h,This file contains definitions internal to the PORT unit
      3. port_rfc.cc,This module contains functions for port emulation entity and RFCOMM communications
      4. port_utils.cc,Port Emulation entity utilities
      5. rfc_int.h,This file contains definitions internal to the RFC unit
      6. rfc_l2cap_if.cc,This file contains L2CAP interface functions
      7. rfc_mx_fsm.cc,This file contains state machine and action routines for multiplexer channel of the RFCOMM unit
      8. rfc_port_fsm.cc,This file contains state machine and action routines for a port of the RFCOMM unit
      9. rfc_port_if.cc,This file contains functions callable by an application running on top of RFCOMM
      10. rfc_ts_frames.cc,This file contains functions to send TS 07.10 frames
      11. rfc_utils.cc,This file contains collection of utility functions used the RFCOMM unit
    18. sdp目录
      1. sdpint.h,This file contains internally used SDP definitions
      2. sdp_api.cc,this file contains SDP interface functions
      3. sdp_db.cc,this file contains functions that handle the database
      4. sdp_discovery.cc,this file contains SDP discovery functions
      5. sdp_main.cc,This file contains the main SDP functions
      6. sdp_server.cc,This file contains functions that handle the SDP server functions. This is mainly dealing with client requests
      7. sdp_utils.cc,This file contains SDP utility functions
    19. smp目录
      1. crypto_toolbox.h,crypto tools
      2. p_256_curvepara.cc, This file contains simple pairing algorithms
      3. p_256_ecc_pp.cc, This file contains simple pairing algorithms using Elliptic Curve Cryptography for private public key
      4. p_256_ecc_pp.h, This file contains simple pairing algorithms using Elliptic Curve Cryptography for private public key
      5. p_256_multprecision.cc, This file contains simple pairing algorithms
      6. p_256_multprecision.h
      7. smp_act.cc,
      8. smp_api.cc, This file contains the implementation of the SMP(Security Manage Protocol) interface used by applications that can run over an SMP.
      9. smp_br_main.cc,
      10. smp_int.h, This file contains internally used SMP definitions
      11. smp_keys.cc, This file contains security manager protocol utility functions
      12. smp_l2c.cc, This file contains functions for the SMP L2Cap interface
      13. smp_main.cc
      14. smp_utils.cc, This file contains functions for the SMP L2CAP utility functions
    20. srvc目录
      1. srvc_dis.cc,gatt DIS(Device Information Service)
      2. srvc_dis_int.h
      3. srvc_eng.cc
      4. srvc_eng_int.h
    21. 1
    22. 2
    23. 21
  23. test目录(蓝牙测试功能及脚本)
    1. bdtool目录,Bluetooth HCI tools for target
    2. hci目录,Bluetooth HCI tools for target
    3. 12
  24. types目录
    1. bluetooth目录
      1. uuid.cc, This class is representing Bluetooth UUIDs across whole stack.
    2. class_of_device.cc,Bluetooth Class of Device
    3. raw_address.cc,Bluetooth Address
    4. 12
    5.  
  25. udrv目录
    1. uipc.cc,UIPC implementation for fluoride. 对于Audio Flinger而言,他能够获取到a2dp的hw module,然后怎才能将数据送至蓝牙协议栈。蓝牙方面已经起了一个线程,专用于发送和接收media的数据,线程名称:btif_media_task.蓝牙与Audio的通信则采用了socket的方式,管理socket的中间文件为:UIPC。因此UIPC主要的作用就是,接收Audio的控制命令转发给bt,接收Audio的音频数据发送给bt。因此UIPC建立了两条socket,分别为:#define A2DP_CTRL_PATH "/data/misc/bluedroid/.a2dp_ctrl",  #define A2DP_DATA_PATH "/data/misc/bluedroid/.a2dp_data"
  26. utils目录,bt utils
  27. vendor_libs(vendor提供的一些蓝牙控制台命令,放置到下面的路径里,/vendor/bin/hw/android.hardware.bluetooth)
    1.  
  28. vnd目录
    1. ble目录,This file contains Broadcom Specific Host Controller Interface definitions.
    2. include目录,Resolvable private address offload VSC specific definitions
  29. 12
  30. 12
  31. 12
  32. 12
  33. 12
  34. 12
  35. 12
  36. 12
  37. 12
  38. 12

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值