当前位置:网站首页>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 :

/img/29/a850ab75b87dea2e9b6581480298bf.jpg

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 !

原网站

版权声明
本文为[yuanyun_ elber]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020629284647.html