Support for btmon in Mynewt

Btmon is very useful tool for HCI messaging monitoring on Linux systems. It helps to understand how Bluetooth host talks to controller, debug issues, implement new features in the host etc. When we have started work with Mynewt we knew that we need tool like this. Of course Mynewt has a way to print all the HCI logs in hex, but decoding it on fly is not the best thing to do. So here it is, Mynewt can be configured in the way that btmon can be used. Detailed information you can find here. Below we show an example how to use it.

For the demo purpose we will use btshell application running on nRF52840 PDK. We will configure it to most convenient option, which is: keep bi-directional console on UART and configure Mynewt to send HCI over RTT to btmon.

Let’s start from creating target:

newt target create btshell
newt target set btshell app=@apache-mynewt-core/apps/btshell bsp=@apache-mynewt-core/hw/bsp/nrf52840pdk build_profile=optimized

We should not forget about boot image:

newt target create nrf52_boot 
newt target set nrf52_boot app=@apache-mynewt-core/apps/boot bsp=@apache-mynewt-core/hw/bsp/nrf52840pdk build_profile=optimized

Now we need to add additional syscfg.yml file into target/btshell/ folder in order to enable HCI over RTT. That file should look like this:

cat targets/btshell/syscfg.yml 

Once configuration is done, let’s build btshell and flash it onto nRF52840pdk

newt build nrf52_boot
newt load nrf52_boot
newt build btshell
newt create-image btshell 1
newt load btshell

Board is ready to play with. Shell is available under /dev/ttyACMx . Test it by typing help

Now we came to most interesting part, HCI tracing. Since btmon can only read data from tty, we need small tool to redirect RTT data to PTY. It is called rtt2pty and is ready for download from our GitHub here. It is delivered as an Eclipse project, so just import it, build and run. Here is how the outcome should look like:

> ./rtt2pty 
Connected to:
  J-Link OB-SAM3U128-V2-NordicSemi compiled Jun  9 2017 14:57:23
  S/N: 683958770
Searching for RTT control block...
Found 3 up-buffers.
Using buffer #1 (size=256)
PTY name is /dev/pts/3

Run btmon:

btmon --tty=/dev/pts/3

And that is it. Now for test let us try to scan for devices around with btshell (don’t forget to set address of device for btshell) e.g.

btshell> set addr=11:11:11:11:11:11
btshell> scan

And here we have, HCI tracing:

Happy tracing!