当前位置:网站首页>Things about Bluetooth development (1) -- starting with packet capturing data
Things about Bluetooth development (1) -- starting with packet capturing data
2022-06-11 00:08:00 【yuanyun_ elber】
From this day onwards , There will be a new column . Try to talk about Bluetooth for beginners , There seems to be a lot of explanations on how to transplant the Bluetooth protocol stack on the Internet btstack Of , But it all means that the transplant is finished , Little knowledge of Bluetooth protocol , Those who talk about Bluetooth protocol on the Internet , Most of them are pasted and pasted , Not in depth , Very unfriendly to beginners .
I want to find a new angle , Try to help beginners get started , Try it , If the effect is good , Can help others , So I still have the motivation to keep writing .
Generally speaking, the protocol stack on the Internet , They all speak in layers , Let's take a look at the diagram of the protocol stack found on the Internet

This is bluelet Block diagram of protocol stack , You look dizzy, don't you , Very unfriendly to beginners .
Here is a little explanation , stay transport layer following , yes controller Part of ,controller It can be a Bluetooth module , that transport The layer depends on the interface of the Bluetooth module , Serial port or usb And so on , For example, it is plugged into the computer usb bluetooth dongle, His transport Namely usb Of , For example, it used to be used a lot in the market ,rda5876 Bluetooth chip , His transport Layer is uart Of .
controller It can also be a baseband embedded in the chip , such transport layer It is a virtual connection layer . The so-called migration protocol , Most of the work , It's here transport layer.
Look at the following Chipset, Each chip includes csr、 qualcomm 、 broadcom 、ti wait , Interfaces are all different , Even if it is the same serial port , Also exist H2,H4,H5 And so on , This driver needs to be written .
then , Each chip is initialized , Need to configure register , This configuration file is called pskey, It is generally customized by various Bluetooth manufacturers hci Command to write to Bluetooth chip , You also need to write another driver .
for instance , The initial baud rate of the serial port is 115200, This baud rate is obviously not enough to support EDR Of 2.1Mbps At a rate , Each chip usually has a special command to configure a new baud rate , Generally need 921600 To meet the needs .
Transport Layer above , It looks very diverse , In fact, the core of Bluetooth has only two layers ,HCI and L2CAP.
HCI yes HOST Protocol stack and controller A bridge of communication .
L2CAP Bluetooth data link layer protocol , Therefore, the application of the upper layer must go through L2CAP, The following text will expand .
The upper layer agreement is generally called profile,profile It is difficult to translate in Chinese , Generally we think it is an application , For example, the most commonly used ones are also the focus of our future introduction ,a2dp agreement , It is the main application of Bluetooth speakers .
for instance SPP, This is what you need to transfer files profile, Let me just say a word here , Apple is disgusting , Apple Mobile SPP It's closed , Want to use SPP The service must be configured additionally Mfi Chip .
But now there is BLE, Basically, they all use BLE It's transmitting data ,SPP A little chicken ribs .
HFP\HSP It's a hands-free telephone protocol .
In fact, any one of these points can be taken out and talked about , for instance HCI, In Bluetooth core spec in , There are 1000 Incoming page :

A large number of data formats 、 standard , If you haven't played Bluetooth , You can see it in the clouds .
So I hope to change my perspective , From the data point of view , From a code point of view , Take a look at the whole process of classic Bluetooth connection , It may be understood with examples , It's easier for beginners .
The data here refers to hcilog, Generally speaking, we study communication , Air bag is very important , But the bag capturing equipment is expensive , and hcilog In general, it is enough , Why? ? Just look at the architecture diagram :

The Bluetooth protocol stack is hci Divided into Host and Controller Two parts , In the narrow sense, the protocol stack refers to Host, Actually Controller There is also a part of the protocol stack , Include LC、LMP wait .
Controller Bluetooth module , Finally, it will be operated on the rf hardware register and other parts , It also includes LMP、LC Layer and other layer protocols , Generally, we don't care about this , The modules provided by the manufacturer are all encapsulated .
therefore Host Upper middle profile All packets that need to be sent or received are sent through HCI Interface and controller Interaction , You can take hci It is understood as the pick-up point of express delivery ,Host The packets that need to be sent are placed at the pick-up point , The rest controller When to send ,controller It will be sent to at the right time according to a link policy peer End , alike ,controller received peer The end of the package will also pass hci Interface send Host.(HCI ACL data package )
So most air bags go through hci Interfaces can be viewed , Except for a few LMP The bag can't see , But it doesn't matter , I'll talk about it later .
HCILOG It is relatively easy to obtain ,Btstack It can be directly grabbed hcilog Of , function BTStack The program , Will generate hci_dump.pklg file , have access to WireShark Open this file .
Specific code can refer to hci_dump_packetlogger_setup_header, Among them, the data format has been transformed , meanwhile printf_timestamp You can attach a timestamp to a packet .
We plug a Bluetooth into the computer dongle, And run for a while btstack, Generated hci_dump.pklg Open it as shown in the figure

In terms of code, we choose Btstack.
Btstack It is an open source and free protocol stack , The key point is : free !
Of course , If it is to be used commercially , It's also a charge .
I used it a few years ago btstack When developing , There are quite a lot of pits , But the problem with this pit , It depends on how you look .
Many companies spend hundreds of thousands to buy commercial agreement stacks , There are fewer pits , But it took a long time to develop , You don't know Bluetooth, either .
I am still in favor of the idea of the Twin Dragons of the Tang Dynasty , To practice level , It is the best way to cultivate war by fighting , You don't fill a few holes , Experience doesn't increase either .
Come back , All these years have passed , We must also look at problems from the perspective of development . The somebody else btstack It has been developing , Look at the support they have now a2dp 了 , Originally, only l2cap The following core layers , Plus one. rfcomm nothing more .
There is no pit , I haven't been there , It's hard to say .
Operating system , If in unix System , need git,make,python Tools such as . If it is windows, need
Mingw64 Of gcc Environmental Science
Don't say anything , Although the road ahead is too sad , Please bless me with a smile !
边栏推荐
- LabVIEW get IMAQ get last event coordinates
- LabVIEW pictures look bright or dark after being cast from 16 bits to 8 bits
- LabVIEW获取Clamp函数找到的所有点的信息
- LabVIEW uses the visa read function to read USB interrupt data
- Why many new websites are not included by search engines
- In the month of safety production, Huangpu launched a publicity campaign for gas safety in shops
- Why is the website snapshot hijacked and tampered with
- [pyGame games] don't look for it. Here comes the leisure game topic - bubble dragon widget - recommendation for leisure game research and development
- vtk.js中vtp下载
- Is the financial management of qiniu school reliable and safe
猜你喜欢

Hyperleger fabric installation

LabVIEW phase locked loop (PLL)

Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)

LabVIEW performs a serial loopback test

What is the workflow of dry goods MapReduce?

Leetcode-560 and subarray with K
![[AI card player] for the first time to see such an](/img/7b/f1965a74733e152dcc542e465112b0.png)
[AI card player] for the first time to see such an "exciting" landowner, the key factor for a high winning rate is

【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)

【Pygame小游戏】Chrome上的小恐龙竟可以用代码玩儿了?它看起来很好玩儿的样子~

Kubernetes 基本介绍及核心组件
随机推荐
OpenResty安装
[pyGame games] in the first month, it broke 100 million to download a masterpiece that is highly integrated with "super casual game features"~
After deepin20 menu startup option, the self-test indicates that iwlwwifi is stopped
LabVIEW pictures look bright or dark after being cast from 16 bits to 8 bits
LabVIEW图片在从16位强制转换为8位后看起来要亮或暗
长投学堂开户安全吗?靠谱吗?
The same customized right-click menu beautification on this site - Xingze V Club
LabVIEW determines the position of the control in the display coordinate system
Baidu quick collection SEO optimization keyword ranking optimization skills
[auto reply or remind assistant] Mom doesn't have to worry about me missing messages any more (10 Line Code Series)
How to check the variable waveform when debugging the program? Look here
How to measure the refresh rate of oscilloscope
Flowable process deployment
Quick sort
LabVIEW获取IMAQ Get Last Event坐标
[untitled]
30 | how to reset the consumer group displacement?
LabVIEW open other exe programs
When leaving the web page, the website displays 404 Not found- starze V Club
[pyGame games] I'm not afraid you can't walk the maze series: the ultimate AI walks the maze. After learning, it will take you to open the door to a new world ~ (with game source code)