其他分享
首页 > 其他分享> > 如何使用Frontline实时抓取蓝牙协议栈HCI LOG

如何使用Frontline实时抓取蓝牙协议栈HCI LOG

作者:互联网

1.前言
在debug蓝牙协议栈时,hci log是非常有用的debug手段,它能提供蓝牙协议层面的交互信息。常用的hci log分析工具有frontline,wireshark,ellisys。常规的做法是在协议栈中实现btsnoop,将hci log按照btsnoop格式生成log文件,这通常需要文件系统的支持。这种方法的缺点很明显,需要文件系统支持,且不支持实时的录制hci log。接下来我要介绍一种使用frontline实时录制hci log的方法,不需要协议栈支持btsnoop以及文件系统,仅需要支持串口通信就可以。协议栈运行平台stm32,hci log传输方式为串口。

2.环境准备
frontline (https://pan.baidu.com/s/1vcf25Sw0a4F8Tek3askRng 提取码:1hkl 也可以去官网下载最新版本)
蓝牙协议栈 (https://github.com/xihua13104/bluetooth_stack.git)
hci logging tool (https://github.com/xihua13104/HCI_Logging_Tool.git)

下载frontline以及hci logging tool,安装好frontline以后,
将liveimport拷贝到hci logging tool vs工程文件同级目录下(替换同名文件):
(liveimport 所在目录:C:\Program Files (x86)\Frontline Test System II\Frontline 15)
在这里插入图片描述
将LiveImportAPI.dll, LiveImportAPI_x64.dll 2个文件分别拷贝到logging tool目录下的Debug & release中(替换同名文件):
(LiveImportAPI.dll, LiveImportAPI_x64.dll所在目录:C:\Program Files (x86)\Frontline Test System II\Frontline 15\Executables\Core)
在这里插入图片描述
在这里插入图片描述
hci log抓取的思路是,协议栈将hci log raw data通过uart发送给上位机hci logging tool,然后logging tool将其透过frontline提供的接口发送给frontline实时的显示出来。
在这里插入图片描述

协议栈发送hci log sample code:
(1)init hci log uart port
uint8_t hw_uart_hci_log_init(uint32_t baud_rate)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //USART3,GPIOB
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //USART3,GPIOB
USART_DeInit(USART3);
//USART3_TX PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);

//Usart1 NVIC ??
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;      
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;            
NVIC_Init(&NVIC_InitStructure);

USART_InitStructure.USART_BaudRate = baud_rate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 
USART_Init(USART3, &USART_InitStructure);

USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);
USART_Cmd(USART3, ENABLE);                    
return HW_ERR_OK;

}
void uart_bt_hci_log_send(uint8_t buf,uint16_t len)
{
uint16_t index;
for(index = 0; index < len ; index++)
{
/
Wait until the last send is complete, then send the data */
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
USART_SendData(USART3,buf[index]);
}
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
//hw_delay_ms(10);
}
(2)hci log 通信协议
#define BT_HCI_LOG_HEADER_LEGNTH 5 //header + direction + sizeof(log_length)
#define BT_HCI_LOG_CMD 0x01
#define BT_HCI_LOG_ACL_OUT 0x02
#define BT_HCI_LOG_ACL_IN 0x04
#define BT_HCI_LOG_EVT 0x08
typedef uint8_t bt_hci_log_type_t;
void bt_hci_log(bt_hci_log_type_t type, uint8_t *log, uint16_t log_length)
{
uint16_t data_tatal_length = 0;
uint16_t index = 0, i = 0;
uint8_t *buf = NULL;
uint8_t check_sum = 0;
data_tatal_length = BT_HCI_LOG_HEADER_LEGNTH + log_length + 1;//1:check sum
buf = (uint8_t *)malloc(data_tatal_length);
if (buf == NULL) {
return;
}
buf[index++] = 0xF5;
buf[index++] = 0x5A;
buf[index++] = type;
buf[index++] = log_length & 0xFF;
buf[index++] = (log_length >> 8) & 0xFF;
for (i = 0;i < log_length;index++, i++) {
buf[index] = log[i];
}
for (i = 0; i < data_tatal_length - 1;i++) {
check_sum += buf[i];
}
buf[index] = check_sum;
uart_bt_hci_log_send(buf, data_tatal_length);

free(buf);

}
(3)发送log数据
uint8_t hci_reset_cmd[] = {0x01, 0x03, 0x0c, 0x00};
int main ()
{
hw_uart_hci_log_init(115200);
while (1)
{
bt_hci_log(BT_HCI_LOG_CMD, &hci_reset_cmd[1], sizeof(hci_reset_cmd) - 1);
hw_delay_ms(1000);
}
}

3.协议栈录制bt hci log
(1)先打开frontline,并点击start capture button:
在这里插入图片描述
(2)双击“CSample”打开hci logging tool (一定要先开启frontline,否则hci logging tool 无法顺利初始化)
(3)根据提示,输入要打开的串口号,如“COM5”:
在这里插入图片描述
(如果没有可用串口,会提示“no available serial port”)

4.效果演示
在这里插入图片描述

标签:HCI,LOG,USART,InitStructure,GPIO,hci,buf,Frontline,log
来源: https://blog.csdn.net/weixin_38177846/article/details/112095160