蓝牙系列五:最简单的开源蓝牙协议BTStack代码分析(1)

对于蓝牙协议栈的理解,最好的办法是找一个最简单的开源协议栈进行学习,BTStack整个协议栈都是C语言编写,非常适合刚入门的同学来学习借鉴。借鉴卫东上老师的蓝牙视频教程。

BTStack协议栈学习。首先来看一下,对于硬件操作,它是如何来进行处理的。在上篇文章中曾说过,在main函数里面它会调用硬件相关的代码,调用操作系统相关的代码。在BTStack中,可以搜索一下main.c,将会发现有很多main.c,都是为于port目录下面。

Main.c (port\esp32\components\btstack)
Main.c (port\ez430-rf2560\src)
Main.c (port\libusb)
Main.c (port\libusb-intel)
Main.c (port\max32630-fthr\src)
Main.c (port\msp-exp430f5438-cc2564b\src)
Main.c (port\msp430f5229lp-cc2564b\src)
Main.c (port\nrf5-zephyr)
Main.c (port\nrf5x)
Main.c (port\pic32-harmony\src)
Main.c (port\posix-h4)
Main.c (port\posix-h4-atwilc3000)
Main.c (port\posix-h4-da14581)
Main.c (port\posix-h4-da14585)
Main.c (port\posix-h4-zephyr)
Main.c (port\posix-h5)
Main.c (port\posix-h5-bcm)
Main.c (port\raspi)
Main.c (port\samv71-xplained-atwilc3000)
Main.c (port\stm32-f103rb-nucleo)
Main.c (port\stm32-f4discovery-cc256x\eclipse-template\src)
Main.c (port\stm32-l053r8-em9304\cubemx-l053r8-em9304\src)
Main.c (port\wiced-h4)
Main.c (port\wiced-h5)
Main.c (port\windows-h4)
Main.c (port\windows-h4-zephyr)
Main.c (port\windows-winusb)
Main.c (port\windows-winusb-intel)

看一下windows,有Main.c (port\windows-h4)、Main.c (port\windows-winusb),使用的是usb口的蓝牙模块。注意后h4表示5线串口的蓝牙模块。

分析Main.c 中的main函数,按照上一篇文章中总结出来的框架,首先找到硬件操作的相关代码,然后再看操作系统先关的代码

1. 硬件相关的代码:

a.使用usb口

分析Main.c (port\windows-winusb)

// setup USB Transport
transport = hci_transport_usb_instance();

const hci_transport_t * hci_transport_usb_instance(void) {
  return &hci_transport_usb;  //返回hci_transport_usb的结构体
}

hci_transport_usb的结构体定义如下:

// get usb singleton
static const hci_transport_t hci_transport_usb = {
  /* const char * name; */ "H2_WINUSB",
  /* void (*init) (const void *transport_config); */ &usb_init,
  /* int (*open)(void); */ &usb_open,
  /* int (*close)(void); */ &usb_close,
  /* void (*register_packet_handler)(void (*handler)(...); */ &usb_register_packet_handler,
  /* int (*can_send_packet_now)(uint8_t packet_type); */ &usb_can_send_packet_now,
  /* int (*send_packet)(...); */ &usb_send_packet,
  /* int (*set_baudrate)(uint32_t baudrate); */ NULL,
  /* void (*reset_link)(void); */ NULL,
#ifdef ENABLE_SCO_OVER_HCI
  /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ usb_set_sco_config,
#else
  /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
#endif
};

在hci_transport_usb结构体中,有初始化函数、有open函数、有注册函数、有发送包的函数等等,这些函数应该就是用来操作硬件的。

在main函数中,返回了一个结构体,以后将使用transport 这个结构体去操作硬件——从硬件里面得到数据或把数据发给硬件。

以上使用的是USB口,如果我使用的是串口呢?硬件操作的相关代码又是怎样的?

b. 使用串口

分析Main.c (port\windows-h4)

main

  const btstack_uart_block_t * uart_driver = btstack_uart_block_windows_instance();

  const hci_transport_t * transport = hci_transport_h4_instance(uart_driver);  //同样是返回一个transport结构体 

  // configure and return h4 singleton

  const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
    btstack_uart = uart_driver;
    return &hci_transport_h4;//返回hci_transport_h4的结构体
  }

hci_transport_h4结构体是什么样的呢?定义如下:

static const hci_transport_t hci_transport_h4 = {
    /* const char * name; */                                        "H4",
    /* void   (*init) (const void *transport_config); */            &hci_transport_h4_init,
    /* int    (*open)(void); */                                     &hci_transport_h4_open,
    /* int    (*close)(void); */                                    &hci_transport_h4_close,
    /* void   (*register_packet_handler)(void (*handler)(...); */   &hci_transport_h4_register_packet_handler,
    /* int    (*can_send_packet_now)(uint8_t packet_type); */       &hci_transport_h4_can_send_now,
    /* int    (*send_packet)(...); */                               &hci_transport_h4_send_packet,
    /* int    (*set_baudrate)(uint32_t baudrate); */                &hci_transport_h4_set_baudrate,
    /* void   (*reset_link)(void); */                               NULL,
    /* void   (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL,
};

注意:btstack_uart这个参数是用来干嘛的呢?

BTStack支持多种接口的蓝牙模块,比如USB口、3线串口、5线串口。对于3线串口和5线串口,它们之间有什么差别呢?

对于3线串口,它只有三条线:TxD、RxD、GND。5线串口比三线串口多了两条线:CTS、RTS,用来控制流量。
使用三线串口和无线串口传输同一个数据时,它们使用的协议不一样。

假设图中红色的部分就是要发送的数据,当使用三线串口时可能给它加上头部、尾部后再发送给硬件,当使用五线串口时可能将数据直接发给硬件。

从这个地方可以产出,无论是三线串口还是五线串口,它们的底层硬件操作都是一样的。因此在硬件的这一层,又抽象出了一个结构体:uart_driver。使用该结构体来操作硬件。

H5协议只是将数据加上各种头部和各种尾部,H4协议也只是对数据进行了某种处理。

因此在main函数中,首先得到了uart_driver,然后再将该结构体作为hci_transport_h4_instance的参数传进去。

看一下hci_transport_h4_open()函数:

hci_transport_h4_open

   btstack_uart->open();//直接调用了btstack_uart的open函数。

从中可以看出,H4、H5协议通过 btstack_uart_block_t结构体来操作硬件。

2. 操作系统相关的代码

a.windows操作系统

分析Main.c (port\windows-winusb)

main

  btstack_run_loop_init(btstack_run_loop_windows_get_instance());

通过btstack_run_loop_windows_get_instance()来获取一个结构体,

/**
* Provide btstack_run_loop_windows instance
*/
const btstack_run_loop_t * btstack_run_loop_windows_get_instance(void){
  return &btstack_run_loop_windows;
}

btstack_run_loop_windows结构体定义如下:定义了操作系统先关的循环函数。

static const btstack_run_loop_t btstack_run_loop_windows = {
    &btstack_run_loop_windows_init,
    &btstack_run_loop_windows_add_data_source,
    &btstack_run_loop_windows_remove_data_source,
    &btstack_run_loop_windows_enable_data_source_callbacks,
    &btstack_run_loop_windows_disable_data_source_callbacks,
    &btstack_run_loop_windows_set_timer,
    &btstack_run_loop_windows_add_timer,
    &btstack_run_loop_windows_remove_timer,
    &btstack_run_loop_windows_execute,
    &btstack_run_loop_windows_dump_timer,
    &btstack_run_loop_windows_get_time_ms,
};

b. linux操作系统

Main.c (port\posix-h4)

main

  btstack_run_loop_init(btstack_run_loop_posix_get_instance());

通过btstack_run_loop_posix_get_instance()来获取一个结构体

/**
* Provide btstack_run_loop_posix instance
*/
const btstack_run_loop_t * btstack_run_loop_posix_get_instance(void){
  return &btstack_run_loop_posix;
}

btstack_run_loop_posix结构体定义如下:定义了操作系统先关的循环函数。

static const btstack_run_loop_t btstack_run_loop_posix = {
    &btstack_run_loop_posix_init,
    &btstack_run_loop_posix_add_data_source,
    &btstack_run_loop_posix_remove_data_source,
    &btstack_run_loop_posix_enable_data_source_callbacks,
    &btstack_run_loop_posix_disable_data_source_callbacks,
    &btstack_run_loop_posix_set_timer,
    &btstack_run_loop_posix_add_timer,
    &btstack_run_loop_posix_remove_timer,
    &btstack_run_loop_posix_execute,
    &btstack_run_loop_posix_dump_timer,
    &btstack_run_loop_posix_get_time_ms,
};

3. 在主循环中读取数据、处理数据,它是如何用代码来实现的呢?

例如:如果我使用H5协议的话,从硬件中得到数据,需要将这个数据的头部去掉,才能得到真正的数据。如果我使用H4协议的话,从硬件中得到的数据就是真正的数据。

如果我使用usb协议的话,得到的数据又需要作另一种处理。从这个地方就可以看出某种端倪来,什么端倪呢?得到数据和处理数据应该绑定在一起。

即使用A协议得到数据,就使用process_A来处理;使用B协议来得到数据,就使用process_B来处理。在BTStack中,又抽象处理另外一个结构体,该结构体就是btstack_data_source。

上面已经提到操作系统相关的代码时,在结构体btstack_run_loop中它有一个函数指针void (*add_data_source)(btstack_data_source_t * data_source),就是给这个循环添加一个数据来源。这个数据来源里面有文件句柄或handle、process函数。以后在循环里面,它可以通过文件句柄或handle中获取数据,得到数据后马上调用它里面的process函数。问题来了,btstack_data_source结构体是在什么时候创建的?显然应该在打开硬件设备时,就会创建这个结构体,并且把这格结构体添加到btstack_run_loop中。

4.数据如何进行处理的呢?

process函数就是数据处理的起点,前面已经说过,对数据的处理分为两部分:一部分是蓝牙协议栈中各个层次的处理,另一个部分是APP的处理。
data_source结构体中有process函数,process函数就是数据处理的起点,在这里会干什么事情呢?它会调用各个层的处理函数,也会调用APP的处理函数

看一下函数usb_process_event_in,在里面会做什么事情呢?
usb_process_event_in
  // notify uppper 通知更上面的层次
  packet_handler(HCI_EVENT_PACKET, hci_event_in_buffer, bytes_read);
  packet_handler是一个函数指针,static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = &usb_dummy_handler;
  该函数指针在哪里被设置呢?
  static void usb_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size))

{
    log_info("registering packet handler");
    packet_handler = handler;
  }
在硬件相关的结构体hci_transport_usb里面,有一个注册函数usb_register_packet_handler

对于usb蓝牙模块,它使用hci_transport_h2_winusb.c文件中抽象出来的hci_transport_usb结构体,在这个结构体里面有usb_register_packet_handler函数,
static void usb_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
  log_info("registering packet handler");
  packet_handler = handler; 对函数指针packet_handler进行赋值。
}
此处的handler是什么?就需要看看谁调用了register_packet_handler函数指针,调用了register_packet_handler函数指针,就相当于调用了usb_register_packet_handler函数
在hci.c文件中的hci_init函数中调用了register_packet_handler函数指针。
hci_init
  // register packet handlers with transport
  transport->register_packet_handler(&packet_handler); 从这个地方可以看出,上面的handler就是hci.c文件中的packet_handler
  再看一下参数packet_handler
  static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
    hci_dump_packet(packet_type, 1, packet, size);
    switch (packet_type) {
      case HCI_EVENT_PACKET:
        event_handler(packet, size);
        break;
      case HCI_ACL_DATA_PACKET:
        acl_handler(packet, size);
        break;
  #ifdef ENABLE_CLASSIC
      case HCI_SCO_DATA_PACKET:
        sco_handler(packet, size);
        break;
  #endif
      default:
        break;
      }
   }

在主循环中,得到数据之后,会调用btstack_data_source中的process函数,在process函数中最终会进行packet_handler = handler这样的赋值操作。最终会调用到hci.c中的packet_handler函数。在该函数中将数据分为了三类:HCI_EVENT_PACKET、HCI_ACL_DATA_PACKET、HCI_SCO_DATA_PACKET。根据类别的不同调用不同的处理函数。

event_handler(packet, size);
hci_emit_event
  // dispatch to all event handlers 分发给所有的事件处理器,那么这些event handler保存在event_handlers链表中。
  btstack_linked_list_iterator_t it;
  btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers);从链表中将handler一个个取出来,调用那些结构体中的callback函数来处理那些数据。
  while (btstack_linked_list_iterator_has_next(&it)){
    btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
    entry->callback(HCI_EVENT_PACKET, 0, event, size);
  }

问题:谁向hci_stack->event_handlers链表中放入handler的呢?

从图中可以看出,上面的各个层都调用了hci_add_event_handler,上面的各层如果对数据感兴趣的话就调用hci_add_event_handler函数在链表中添加自己的处理函数。

void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
  btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 向event_handler链表中添加了btstack_packet_callback_registration_t结构体,这个结构体是什么样的呢?
}

typedef struct {
  btstack_linked_item_t item;
  btstack_packet_handler_t callback;  有callback函数,刚好与上面对应起来。
} btstack_packet_callback_registration_t;

5. 谁来启动数据传输?

hci_power_control会启动蓝牙模块,向蓝牙模块发送第一个数据,以后所有的数据都会在主循环中进行的,收到数据后将调用process函数。

  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 蓝牙协议BTstack是一种为蓝牙设备开发的基础框架。它旨在为使用低功耗蓝牙协议BLE)和传统蓝牙协议(BR / EDR)的设备提供通用接口。 BTstack提供了一组API,使开发人员可以使用不同平台上的相同代码来处理蓝牙样式的应用程序。 BTstack提供的API包括协议栈初始化,L2CAP,RFCOMM,SDP,ATT / GATT等。 BTstack的另一个优点是它是一个开源项目,可以在许多平台上使用。它已被移植到多种硬件平台和操作系统中,例如ARM Cortex-M3 / M4(例如STM32),ATmega,Arduino,Raspberry Pi,Windows,Linux,macOS和iOS。 BTstack的应用广泛,包括智能手机,平板电脑,手表,耳机,音响,传感器和医疗设备。随着IoT设备的普及,BTstack将越来越受到重视。 总之,BTstack是一个功能强大、开源、跨平台的蓝牙协议栈,为开发人员提供了通用API,使他们可以在不同的平台上开发相同的应用程序。它是蓝牙设备开发者的重要工具之一。 ### 回答2: 蓝牙协议栈是指一套硬件和软件技术,用于使不同设备间的数据传输更加方便快捷。蓝牙协议栈的运行方式是通过多层协议交互实现的,向上层应用提供数据传输的支持,向下层硬件提供调用接口。 BTstack是一种开源蓝牙协议栈,提供了高效、可靠和易于使用的终端设备的蓝牙连接。BTstack支持各种蓝牙协议,包括BLE、HFP、A2DP、AVRCP和SPP等,可以在不同的操作系统和硬件平台上使用。BTstack的优点在于它是可移植的,可以快速适应各种不同的蓝牙设备。 BTstack还支持多个蓝牙连接,并提供了稳定的设备连接,具备智能重连和设备发现功能,同时还支持多个蓝牙配置文件。BTstack还支持单片机,特别是ARM Cortex-M处理器,以及一些嵌入式系统平台。 总之,BTstack是一种强大的、稳定的、高度可移植的蓝牙协议栈,可以帮助开发者快速开发出高效、可靠的蓝牙连接应用。 ### 回答3: 蓝牙协议栈是用来实现蓝牙通信协议的软件模块,因此需要进行一系列的层次化操作,这些层次可以分为物理层,链路层,传输层,应用层。而btstack则是其中一个基于C语言的开源协议栈。 btstack采用了模块化的设计,每个模块之间相对独立,且易于扩展。可以在移植到不同平台上时,很容易添加某些特定的硬件驱动和操作系统。同时,btstack也提供了丰富的API接口,便于快速实现各种蓝牙应用,例如数据传输、音频传输、打印机、键盘/鼠标等等。 除此之外,btstack还支持免费开源蓝牙协议栈,可供个人开源项目和商业开发者使用。同步提供了符合不同使用场景的两种许可证:BSD和GPLv2。且支持大部分操作系统,包括Windows、MacOS、Linux、Android、IOS 和Windows Phone等,在各个平台上表现出色。 总而言之,btstack是个高度模块化、可移植性高、及具有完整的特性和相同的性能的优秀蓝牙堆栈。它极大地推进了蓝牙技术的发展,且为蓝牙应用开发者提供了更多的灵活自由,是目前蓝牙通信领域应用很广泛的协议栈。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值