bashshellbluetooth-lowenergyhci

Read/parse streamed output from BTMON in native bash?


I'm attempting to parse stream output from btmon in realtime. An lescan process is running in the background: sudo hcitool lescan &.

After substantial testing, I'm at a loss for words because the following script, when executed by root in a first session:

while read -r line; do
echo "$line"
done < <(btmon 2>&1)

Does not print the same lines as:

sudo btmon 2>&1

When executed from a separate shell session. Fewer lines are printed from the first and output lags by sometimes minutes.

What is going on? Any input or advice?


EDIT: Example output. All three sessions are simultaneous and the expected result is identical output. All were stopped at the same time.

sudo hcitool lescan

LE Scan ...
49:D3:E5:61:AE:36 (unknown)
49:D3:E5:61:AE:36 (unknown)
28:39:5E:49:DB:23 (unknown)
28:39:5E:49:DB:23 [TV] Samsung 9 Series (65)
5D:C2:ED:93:59:1F (unknown)
6A:46:64:F2:25:57 (unknown)
6A:46:64:F2:25:57 (unknown)
78:D0:8A:A5:8B:43 (unknown)
78:D0:8A:A5:8B:43 (unknown)
33:CA:22:86:F5:4D (unknown)
C8:69:CD:6A:89:2A (unknown)
6A:4E:F6:F6:BB:8C (unknown)
6A:4E:F6:F6:BB:8C (unknown)
C8:69:CD:6A:89:2A (unknown)
5D:C2:ED:93:59:1F (unknown)
7C:D1:C3:DA:91:14 (unknown)
7C:D1:C3:DA:91:14 (unknown)
6D:D2:11:18:37:F1 (unknown)
6D:D2:11:18:37:F1 (unknown)
F8:77:B8:8A:01:C1 (unknown)
FF:FF:C0:22:6B:36 (unknown)

sudo btmon 2>&1

Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l)                                                                      0.563610
= Note: Bluetooth subsystem version 2.22                                                                     0.563625
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0)                                                    [hci0] 0.563632
= Open Index: B8:27:EB:C8:9E:A6                                                                       [hci0] 0.563634
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation)                                                [hci0] 0.563638
@ MGMT Open: bluetoothd (privileged) version 1.14                                                   {0x0001} 0.563644
@ MGMT Open: btmon (privileged) version 1.14                                                        {0x0002} 0.563788
@ MGMT Open: btmon (privileged) version 1.14                                                        {0x0003} 2.149663
@ RAW Open: hcitool (privileged) version 2.22                                                      {0x0004} 11.269034
@ RAW Close: hcitool                                                                               {0x0004} 11.270311
@ RAW Open: hcitool (privileged) version 2.22                                               {0x0004} [hci0] 11.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                           [hci0] 11.272202
        Type: Active (0x01)
        Interval: 10.000 msec (0x0010)
        Window: 10.000 msec (0x0010)
        Own address type: Public (0x00)
        Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4                                                          [hci0] 11.272708
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                               [hci0] 11.275396
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                          [hci0] 11.276126
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35                                                            [hci0] 11.287680
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Random (0x01)
        Address: 49:D3:E5:61:AE:36 (Resolvable)
        Data length: 23
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        Company: Apple, Inc. (76)
          Type: Unknown (12)
          Data: 0063169caf541000a9cd5d9975f7
        RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.288789
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Random (0x01)
        Address: 49:D3:E5:61:AE:36 (Resolvable)
        Data length: 0
        RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43                                                            [hci0] 11.301135
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
        Data length: 31
        Flags: 0x1a
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Samsung Electronics Co. Ltd. (117)
          Data: 420401017e28395e49db232a395e49db2201000000000000
        RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40                                                            [hci0] 11.305925
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Public (0x00)
        Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
        Data length: 28
        Name (short): [TV] Samsung 9 Series (65)
        RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 11.311464
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Random (0x01)
        Address: 5D:C2:ED:93:59:1F (Resolvable)
        Data length: 14
        Flags: 0x1a
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Apple, Inc. (76)
          Type: Unknown (16)
          Data: 071c6824b9
        RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34                                                            [hci0] 11.329588
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Random (0x01)
        Address: 6A:46:64:F2:25:57 (Resolvable)
        Data length: 22
        Flags: 0x1a
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Apple, Inc. (76)
          Type: Unknown (14)
          Data: 01004f000702
          Type: Unknown (16)
          Data: 0a1cca57b2
        RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.332246
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Random (0x01)
        Address: 6A:46:64:F2:25:57 (Resolvable)
        Data length: 0
        RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 11.373918
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Random (0x01)
        Address: 78:D0:8A:A5:8B:43 (Resolvable)
        Data length: 14
        Flags: 0x1a
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Apple, Inc. (76)
          Type: Unknown (16)
          Data: 031c429af9
        RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.376067
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Random (0x01)
        Address: 78:D0:8A:A5:8B:43 (Resolvable)
        Data length: 0
        RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27                                                            [hci0] 11.385151
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
        Address type: Random (0x01)
        Address: 33:CA:22:86:F5:4D (Non-Resolvable)
        Data length: 15
        Flags: 0x1b
          LE Limited Discoverable Mode
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Apple, Inc. (76)
          Type: Apple TV (9)
          Data: 032bc0a8012b
        RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23                                                            [hci0] 11.400609
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: C8:69:CD:6A:89:2A (Apple, Inc.)
        Data length: 11
        Flags: 0x1a
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Apple, Inc. (76)
          Type: Unknown (16)
          Data: 0104
        RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32                                                            [hci0] 11.486780
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Random (0x01)
        Address: 6A:4E:F6:F6:BB:8C (Resolvable)
        Data length: 20
        Flags: 0x1a
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Apple, Inc. (76)
          Type: Unknown (13)
          Data: 53d559a3
          Type: Unknown (16)
          Data: 031cf2b2a6
        RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.489229
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Random (0x01)
        Address: 6A:4E:F6:F6:BB:8C (Resolvable)
        Data length: 0
        RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.585662
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Public (0x00)
        Address: C8:69:CD:6A:89:2A (Apple, Inc.)
        Data length: 0
        RSSI: -82 dBm (0xae)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 11.855660
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Random (0x01)
        Address: 5D:C2:ED:93:59:1F (Resolvable)
        Data length: 0
        RSSI: -90 dBm (0xa6)
> HCI Event: LE Meta Event (0x3e) plen 23                                                            [hci0] 11.999393
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
        Data length: 11
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        Company: Apple, Inc. (76)
          Type: Unknown (16)
          Data: 0b08
        RSSI: -97 dBm (0x9f)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 12.191914
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Public (0x00)
        Address: 7C:D1:C3:DA:91:14 (Apple, Inc.)
        Data length: 0
        RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 12.772156
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Random (0x01)
        Address: 6D:D2:11:18:37:F1 (Resolvable)
        Data length: 14
        Flags: 0x1a
          LE General Discoverable Mode
          Simultaneous LE and BR/EDR (Controller)
          Simultaneous LE and BR/EDR (Host)
        Company: Apple, Inc. (76)
          Type: Unknown (16)
          Data: 0198b24d86
        RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 12                                                            [hci0] 12.773961
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Random (0x01)
        Address: 6D:D2:11:18:37:F1 (Resolvable)
        Data length: 0
        RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 40                                                            [hci0] 13.347879
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
        Address type: Public (0x00)
        Address: F8:77:B8:8A:01:C1 (Samsung Electronics Co.,Ltd)
        Data length: 28
        Company: Samsung Electronics Co. Ltd. (117)
          Data: 4204018060f877b88a01c1fa77b88a01c001000000000000
        RSSI: -100 dBm (0x9c)
> HCI Event: LE Meta Event (0x3e) plen 26                                                            [hci0] 14.016707
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: FF:FF:C0:22:6B:36 (OUI FF-FF-C0)
        Data length: 14
        Flags: 0x05
          LE Limited Discoverable Mode
          BR/EDR Not Supported
        TX power: 0 dBm
        Appearance: Keyboard (0x03c1)
        16-bit Service UUIDs (partial): 1 entry
          Unknown (0xffe0)
        RSSI: -90 dBm (0xa6)

And, finally, the script:

Bluetooth monitor ver 5.43
= Note: Linux version 4.14.71+ (armv6l)                                0.149535
= Note: Bluetooth subsystem version 2.22                               0.149551
= New Index: B8:27:EB:C8:9E:A6 (Primary,UART,hci0)              [hci0] 0.149556
= Open Index: B8:27:EB:C8:9E:A6                                 [hci0] 0.149559
= Index Info: B8:27:EB:C8:9E:A6 (Broadcom Corporation)          [hci0] 0.149563
@ MGMT Open: btmon (privileged) version 1.14                  {0x0002} 0.149569
@ MGMT Open: bluetoothd (privileged) version 1.14             {0x0001} 0.149573
@ MGMT Open: btmon (privileged) version 1.14                  {0x0003} 0.149663
@ RAW Open: hcitool (privileged) version 2.22                 {0x0004} 9.269034
@ RAW Close: hcitool                                          {0x0004} 9.270311
@ RAW Open: hcitool (privileged) version 2.22          {0x0004} [hci0] 9.271277
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7      [hci0] 9.272202
Type: Active (0x01)
Interval: 10.000 msec (0x0010)
Window: 10.000 msec (0x0010)
Own address type: Public (0x00)
Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4                     [hci0] 9.272708
LE Set Scan Parameters (0x08|0x000b) ncmd 1
Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2          [hci0] 9.275396
Scanning: Enabled (0x01)
Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                     [hci0] 9.276126
LE Set Scan Enable (0x08|0x000c) ncmd 1
Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 35                       [hci0] 9.287680
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 23
Flags: 0x06
LE General Discoverable Mode
BR/EDR Not Supported
Company: Apple, Inc. (76)
Type: Unknown (12)
Data: 0063169caf541000a9cd5d9975f7
RSSI: -86 dBm (0xaa)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.288789
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 49:D3:E5:61:AE:36 (Resolvable)
Data length: 0
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 43                       [hci0] 9.301135
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 31
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Samsung Electronics Co. Ltd. (117)
Data: 420401017e28395e49db232a395e49db2201000000000000
RSSI: -71 dBm (0xb9)
> HCI Event: LE Meta Event (0x3e) plen 40                       [hci0] 9.305925
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Public (0x00)
Address: 28:39:5E:49:DB:23 (OUI 28-39-5E)
Data length: 28
Name (short): [TV] Samsung 9 Series (65)
RSSI: -72 dBm (0xb8)
> HCI Event: LE Meta Event (0x3e) plen 26                       [hci0] 9.311464
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 5D:C2:ED:93:59:1F (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 071c6824b9
RSSI: -92 dBm (0xa4)
> HCI Event: LE Meta Event (0x3e) plen 34                       [hci0] 9.329588
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 22
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (14)
Data: 01004f000702
Type: Unknown (16)
Data: 0a1cca57b2
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.332246
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:46:64:F2:25:57 (Resolvable)
Data length: 0
RSSI: -77 dBm (0xb3)
> HCI Event: LE Meta Event (0x3e) plen 26                       [hci0] 9.373918
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 14
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 031c429af9
RSSI: -88 dBm (0xa8)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.376067
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 78:D0:8A:A5:8B:43 (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 27                       [hci0] 9.385151
LE Advertising Report (0x02)
Num reports: 1
Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
Address type: Random (0x01)
Address: 33:CA:22:86:F5:4D (Non-Resolvable)
Data length: 15
Flags: 0x1b
LE Limited Discoverable Mode
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Apple TV (9)
Data: 032bc0a8012b
RSSI: -68 dBm (0xbc)
> HCI Event: LE Meta Event (0x3e) plen 23                       [hci0] 9.400609
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Public (0x00)
Address: C8:69:CD:6A:89:2A (Apple, Inc.)
Data length: 11
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (16)
Data: 0104
RSSI: -80 dBm (0xb0)
> HCI Event: LE Meta Event (0x3e) plen 32                       [hci0] 9.486780
LE Advertising Report (0x02)
Num reports: 1
Event type: Connectable undirected - ADV_IND (0x00)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 20
Flags: 0x1a
LE General Discoverable Mode
Simultaneous LE and BR/EDR (Controller)
Simultaneous LE and BR/EDR (Host)
Company: Apple, Inc. (76)
Type: Unknown (13)
Data: 53d559a3
Type: Unknown (16)
Data: 031cf2b2a6
RSSI: -85 dBm (0xab)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci0] 9.489229
LE Advertising Report (0x02)
Num reports: 1
Event type: Scan response - SCAN_RSP (0x04)
Address type: Random (0x01)
Address: 6A:4E:F6:F6:BB:8C (Resolvable)
Data length: 0
RSSI: -87 dBm (0xa9)

As shown, the script is lagging substantially. If I wait until the hcitool settles down, the script does not catch up. Output just ... stops.

I'm baffled.


Solution

  • It looks like you've got three separate issues here:

    In the interests of also appearing to be a TTY rather than a pipe to btmon, the below also uses unbuffer (shipped as part of expect) rather than the lower-overhead GNU coreutils tool stdbuf -o0:

    Consider instead:

    while IFS= read -r line; do
      printf '%s\n' "$line"
    done < <(unbuffer btmon 2>&1)