currantlabs / ble

BSD 3-Clause "New" or "Revised" License
209 stars 177 forks source link

Cannot explore devices on Raspberry Pi #29

Open blin4444 opened 7 years ago

blin4444 commented 7 years ago

I'm trying to run the explorer program under Raspbian on a Raspberry Pi. When I attempt to connect to a device, this happens:

root@raspberrypi:/home/pi/bluetooth/examples/basic# go run explorer/main.go -addr EB:3F:D1:59:21:80 Scanning for 5s... Discovering profile... 2017/01/20 23:51:51 can't discover profile: can't discover services: ATT request failed: input channel closed: io: read/write on closed pipe

I've tried to explore two different Bluetooth LE devices (Surface Mouse and a sensor module), with the same result.

roylee17 commented 7 years ago

Are you familiar with hcidump? We'll need the log for the debugging.

It seems to be disconnected by the remote peripheral. It might be handy to reproduce this with example/blesh. So we can break it into two steps, and see what happened at each steps.

$ blesh sh
blesh > connect -a EB:3F:D1:59:21:80
blesh > explorer
deadprogram commented 7 years ago

Interestingly, I have been able to successfully run BLE on my Raspberry Pi 3 with built-in BLE, using the latest Raspbian OS. @blin4444 what kind of hardware interface are you using, and which model raspi?

blin4444 commented 7 years ago

I have a Raspberry Pi 3 Model B V1.2. @deadprogram what does hardware interface mean? Like hci0 / built-in Bluetooth hardware on Raspberry Pi?

I have BlueZ 5.43 but have stopped the server as per the instructions in the README on currantlabs/gatt. Have also run: sudo hciconfig down.

I could connect to both devices using the PayPal gatt library explorer.go after changing line 99 in adv.go to d := b[2 : 2+l] from d := b[2 : 1+l]. It still eventually crashed for the Surface Mouse.

Originally tried on: Linux raspberrypi 4.4.11-v7+ #888 SMP Mon May 23 20:10:33 BST 2016 armv7l GNU/Linux.

Updated to: Linux raspberrypi 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l GNU/Linux with no difference in results.

Attempting to connect to a Surface Mouse:

blesh command output:

Initializing device ...
Dialing to specified address: eb:3f:d1:59:21:80
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x18090]

goroutine 50 [running]:
panic(0x1fc1f0, 0x10624008)
    /usr/local/go/src/runtime/panic.go:500 +0x33c
main.cmdConnect.func1(0x0, 0x0)
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:261 +0x1c
created by main.cmdConnect
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:265 +0x348
HCI sniffer - Bluetooth packet analyzer ver 5.43
btsnoop version: 1 datalink type: 1002
< HCI Command: Read Local Supported Features (0x04|0x0003) plen 0
> HCI Event: Command Complete (0x0e) plen 12
    Read Local Supported Features (0x04|0x0003) ncmd 1
    status 0x00
    Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
< HCI Command: Read Local Version Information (0x04|0x0001) plen 0
> HCI Event: Command Complete (0x0e) plen 12
    Read Local Version Information (0x04|0x0001) ncmd 1
    status 0x00
    HCI Version: 4.1 (0x7) HCI Revision: 0xb6
    LMP Version: 4.1 (0x7) LMP Subversion: 0x2209
    Manufacturer: Broadcom Corporation (15)
< HCI Command: Read BD ADDR (0x04|0x0009) plen 0
> HCI Event: Command Complete (0x0e) plen 10
    Read BD ADDR (0x04|0x0009) ncmd 1
    status 0x00 bdaddr B8:27:EB:B0:94:27
< HCI Command: Read Buffer Size (0x04|0x0005) plen 0
> HCI Event: Command Complete (0x0e) plen 11
    Read Buffer Size (0x04|0x0005) ncmd 1
    status 0x00
    ACL MTU 1021:8 SCO MTU 64:1
< HCI Command: Read Class of Device (0x03|0x0023) plen 0
> HCI Event: Command Complete (0x0e) plen 7
    Read Class of Device (0x03|0x0023) ncmd 1
    status 0x00 class 0x000000
< HCI Command: Read Local Name (0x03|0x0014) plen 0
> HCI Event: Command Complete (0x0e) plen 252
    Read Local Name (0x03|0x0014) ncmd 1
    status 0x00 name 'BCM43438A1 37.4MHz Raspberry Pi 3-0043O'
< HCI Command: Read Voice Setting (0x03|0x0025) plen 0
> HCI Event: Command Complete (0x0e) plen 6
    Read Voice Setting (0x03|0x0025) ncmd 1
    status 0x00 voice setting 0x0060
< HCI Command: Read Number of Supported IAC (0x03|0x0038) plen 0
> HCI Event: Command Complete (0x0e) plen 5
    Read Number of Supported IAC (0x03|0x0038) ncmd 1
< HCI Command: Read Current IAC LAP (0x03|0x0039) plen 0
> HCI Event: Command Complete (0x0e) plen 8
    Read Current IAC LAP (0x03|0x0039) ncmd 1
    IAC 0x9e8b33 (General Inquiry Access Code)
< HCI Command: Set Event Filter (0x03|0x0005) plen 1
    type 0 condition 0
    Clear all filters
> HCI Event: Command Complete (0x0e) plen 4
    Set Event Filter (0x03|0x0005) ncmd 1
    status 0x00
< HCI Command: Write Connection Accept Timeout (0x03|0x0016) plen 2
    timeout 32000
> HCI Event: Command Complete (0x0e) plen 4
    Write Connection Accept Timeout (0x03|0x0016) ncmd 1
    status 0x00
< HCI Command: LE Read Buffer Size (0x08|0x0002) plen 0
> HCI Event: Command Complete (0x0e) plen 7
    LE Read Buffer Size (0x08|0x0002) ncmd 1
    status 0x00 pktlen 0x00fb maxpkt 0x08
< HCI Command: LE Read Local Supported Features (0x08|0x0003) plen 0
> HCI Event: Command Complete (0x0e) plen 12
    LE Read Local Supported Features (0x08|0x0003) ncmd 1
    status 0x00 features 0x1f00000000000000 (Link Layer supports LE Encryption)
< HCI Command: LE Read Supported States (0x08|0x001c) plen 0
> HCI Event: Command Complete (0x0e) plen 12
    LE Read Supported States (0x08|0x001c) ncmd 1
< HCI Command: Read Local Supported Commands (0x04|0x0002) plen 0
> HCI Event: Command Complete (0x0e) plen 68
    Read Local Supported Commands (0x04|0x0002) ncmd 1
    status 0x00
    Commands: ffffff03ccffefffffffec1ff20fe8fe3ff78fff1c00040061f7ffff7ff8ffff
              ff3f
< HCI Command: Write Extended Inquiry Response (0x03|0x0052) plen 241
    fec 0x00
> HCI Event: Command Complete (0x0e) plen 4
    Write Extended Inquiry Response (0x03|0x0052) ncmd 1
    status 0x00
< HCI Command: Write Inquiry Mode (0x03|0x0045) plen 1
    mode 2
> HCI Event: Command Complete (0x0e) plen 4
    Write Inquiry Mode (0x03|0x0045) ncmd 1
    status 0x00
< HCI Command: Read Inquiry Response Transmit Power Level (0x03|0x0058) plen 0
> HCI Event: Command Complete (0x0e) plen 5
    Read Inquiry Response Transmit Power Level (0x03|0x0058) ncmd 1
    status 0x00 level 0
< HCI Command: Read Local Extended Features (0x04|0x0004) plen 1
    page 1
> HCI Event: Command Complete (0x0e) plen 14
    Read Local Extended Features (0x04|0x0004) ncmd 1
    status 0x00 page 1 max 2
    Features: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
< HCI Command: Set Event Mask (0x03|0x0001) plen 8
    Mask: 0xfffffbff07f8bf3d
> HCI Event: Command Complete (0x0e) plen 4
    Set Event Mask (0x03|0x0001) ncmd 1
    status 0x00
< HCI Command: Read Stored Link Key (0x03|0x000d) plen 7
    bdaddr 00:00:00:00:00:00 all 1
> HCI Event: Command Complete (0x0e) plen 8
    Read Stored Link Key (0x03|0x000d) ncmd 1
    status 0x00 max 1 num 0
< HCI Command: Write Default Link Policy Settings (0x02|0x000f) plen 2
    policy 0x05
    Link policy: RSWITCH SNIFF 
> HCI Event: Command Complete (0x0e) plen 4
    Write Default Link Policy Settings (0x02|0x000f) ncmd 1
    status 0x00
< HCI Command: Read Page Scan Activity (0x03|0x001b) plen 0
> HCI Event: Command Complete (0x0e) plen 8
    Read Page Scan Activity (0x03|0x001b) ncmd 1
    status 0x00 interval 2048 window 18
< HCI Command: Read Page Scan Type (0x03|0x0046) plen 0
> HCI Event: Command Complete (0x0e) plen 5
    Read Page Scan Type (0x03|0x0046) ncmd 1
< HCI Command: LE Set Event Mask (0x08|0x0001) plen 8
    mask 0x3f00000000000000 (Reserved)
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Event Mask (0x08|0x0001) ncmd 1
    status 0x00
< HCI Command: LE Read Advertising Channel Tx Power (0x08|0x0007) plen 0
> HCI Event: Command Complete (0x0e) plen 5
    LE Read Advertising Channel Tx Power (0x08|0x0007) ncmd 1
    status 0x00 level 0xc (dBm)
< HCI Command: LE Read White List Size (0x08|0x000f) plen 0
> HCI Event: Command Complete (0x0e) plen 5
    LE Read White List Size (0x08|0x000f) ncmd 1
< HCI Command: LE Clear White List (0x08|0x0010) plen 0
> HCI Event: Command Complete (0x0e) plen 4
    LE Clear White List (0x08|0x0010) ncmd 1
    status 0x00
< HCI Command: Write LE Host Supported (0x03|0x006d) plen 2
> HCI Event: Command Complete (0x0e) plen 4
    Write LE Host Supported (0x03|0x006d) ncmd 1
< HCI Command: Read Local Extended Features (0x04|0x0004) plen 1
    page 2
> HCI Event: Command Complete (0x0e) plen 14
    Read Local Extended Features (0x04|0x0004) ncmd 1
    status 0x00 page 2 max 2
    Features: 0x13 0x03 0x00 0x00 0x00 0x00 0x00 0x00
< HCI Command: Delete Stored Link Key (0x03|0x0012) plen 7
    bdaddr 00:00:00:00:00:00 all 1
> HCI Event: Command Complete (0x0e) plen 6
    Delete Stored Link Key (0x03|0x0012) ncmd 1
    status 0x00 deleted 0
< HCI Command: Set Event Mask Page 2 (0x03|0x0063) plen 8
    Mask: 0x00c0bf0000000000
> HCI Event: Command Complete (0x0e) plen 4
    Set Event Mask Page 2 (0x03|0x0063) ncmd 1
    status 0x00
< HCI Command: Unknown (0x04|0x000b) plen 0
> HCI Event: Command Complete (0x0e) plen 12
    Unknown (0x04|0x000b) ncmd 1
< HCI Command: Unknown (0x05|0x000c) plen 0
> HCI Event: Command Complete (0x0e) plen 15
    Unknown (0x05|0x000c) ncmd 1
< HCI Command: Reset (0x03|0x0003) plen 0
> HCI Event: Command Complete (0x0e) plen 4
    Reset (0x03|0x0003) ncmd 1
    status 0x00
< HCI Command: Reset (0x03|0x0003) plen 0
> HCI Event: Command Complete (0x0e) plen 4
    Reset (0x03|0x0003) ncmd 1
    status 0x00
< HCI Command: Read BD ADDR (0x04|0x0009) plen 0
> HCI Event: Command Complete (0x0e) plen 10
    Read BD ADDR (0x04|0x0009) ncmd 1
    status 0x00 bdaddr B8:27:EB:B0:94:27
< HCI Command: Read Buffer Size (0x04|0x0005) plen 0
> HCI Event: Command Complete (0x0e) plen 11
    Read Buffer Size (0x04|0x0005) ncmd 1
    status 0x00
    ACL MTU 1021:8 SCO MTU 64:1
< HCI Command: LE Read Buffer Size (0x08|0x0002) plen 0
> HCI Event: Command Complete (0x0e) plen 7
    LE Read Buffer Size (0x08|0x0002) ncmd 1
    status 0x00 pktlen 0x00fb maxpkt 0x08
< HCI Command: LE Read Advertising Channel Tx Power (0x08|0x0007) plen 0
> HCI Event: Command Complete (0x0e) plen 5
    LE Read Advertising Channel Tx Power (0x08|0x0007) ncmd 1
    status 0x00 level 0xc (dBm)
< HCI Command: LE Set Event Mask (0x08|0x0001) plen 8
    mask 0x1f00000000000000 (Reserved)
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Event Mask (0x08|0x0001) ncmd 1
    status 0x00
< HCI Command: Set Event Mask (0x03|0x0001) plen 8
    Mask: 0xfffffbff07f8bf3d
> HCI Event: Command Complete (0x0e) plen 4
    Set Event Mask (0x03|0x0001) ncmd 1
    status 0x00
< HCI Command: Write LE Host Supported (0x03|0x006d) plen 2
> HCI Event: Command Complete (0x0e) plen 4
    Write LE Host Supported (0x03|0x006d) ncmd 1
< HCI Command: LE Set Advertising Parameters (0x08|0x0006) plen 15
    min 20.000ms, max 20.000ms
    type 0x00 (ADV_IND - Connectable undirected advertising) ownbdaddr 0x00 (Public)
    directbdaddr 0x00 (Public) 00:00:00:00:00:00
    channelmap 0x07 filterpolicy 0x00 (Allow scan from any, connection from any)
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Advertising Parameters (0x08|0x0006) ncmd 1
    status 0x00
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7
    type 0x01 (active)
    interval 2.500ms window 2.500ms
    own address: 0x00 (Public) policy: All
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Parameters (0x08|0x000b) ncmd 1
    status 0x00
< HCI Command: LE Set Advertising Parameters (0x08|0x0006) plen 15
    min 20.000ms, max 20.000ms
    type 0x00 (ADV_IND - Connectable undirected advertising) ownbdaddr 0x00 (Public)
    directbdaddr 0x00 (Public) 00:00:00:00:00:00
    channelmap 0x07 filterpolicy 0x00 (Allow scan from any, connection from any)
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Advertising Parameters (0x08|0x0006) ncmd 1
    status 0x00
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7
    type 0x01 (active)
    interval 2.500ms window 2.500ms
    own address: 0x00 (Public) policy: All
> HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Parameters (0x08|0x000b) ncmd 1
    status 0x00
< HCI Command: LE Create Connection (0x08|0x000d) plen 25
    bdaddr EB:3F:D1:59:21:80 type 0
    interval 4 window 4 initiator_filter 0
    own_bdaddr_type 0 min_interval 6 max_interval 6
    latency 0 supervision_to 72 min_ce 0 max_ce 0
> HCI Event: Command Status (0x0f) plen 4
    LE Create Connection (0x08|0x000d) status 0x00 ncmd 1
< HCI Command: Reset (0x03|0x0003) plen 0
> HCI Event: Command Complete (0x0e) plen 4
    Reset (0x03|0x0003) ncmd 1
    status 0x00

Attempting to connect to a sensor module:

blesh > connect -a 28:A1:83:31:16:77
Dialing to specified address: 28:a1:83:31:16:77
blesh > explore
Discovering profile...
can't discover services: ATT request timeout: req timeout

can't discover profile
main.cmdDiscover
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:297
main.cmdExplore
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:311
github.com/urfave/cli.HandleAction
    /home/pi/Source/src/github.com/urfave/cli/app.go:485
github.com/urfave/cli.Command.Run
    /home/pi/Source/src/github.com/urfave/cli/command.go:207
github.com/urfave/cli.(*App).Run
    /home/pi/Source/src/github.com/urfave/cli/app.go:250
main.cmdShell
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:422
main.main.func1
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:147
github.com/urfave/cli.HandleAction
    /home/pi/Source/src/github.com/urfave/cli/app.go:487
github.com/urfave/cli.Command.Run
    /home/pi/Source/src/github.com/urfave/cli/command.go:207
github.com/urfave/cli.(*App).Run
    /home/pi/Source/src/github.com/urfave/cli/app.go:250
main.main
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:152
runtime.main
    /usr/local/go/src/runtime/proc.go:183
runtime.goexit
    /usr/local/go/src/runtime/asm_arm.s:998
can't discover profile
main.cmdExplore
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:312
github.com/urfave/cli.HandleAction
    /home/pi/Source/src/github.com/urfave/cli/app.go:485
github.com/urfave/cli.Command.Run
    /home/pi/Source/src/github.com/urfave/cli/command.go:207
github.com/urfave/cli.(*App).Run
    /home/pi/Source/src/github.com/urfave/cli/app.go:250
main.cmdShell
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:422
main.main.func1
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:147
github.com/urfave/cli.HandleAction
    /home/pi/Source/src/github.com/urfave/cli/app.go:487
github.com/urfave/cli.Command.Run
    /home/pi/Source/src/github.com/urfave/cli/command.go:207
github.com/urfave/cli.(*App).Run
    /home/pi/Source/src/github.com/urfave/cli/app.go:250
main.main
    /home/pi/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:152
runtime.main
    /usr/local/go/src/runtime/proc.go:183
runtime.goexit
    /usr/local/go/src/runtime/asm_arm.s:998

Connecting to the sensor module on macOS sometimes works:

blesh > connect -addr ee8a069ab8a24b50aecff325dcd22ffa
Dialing to specified address: ee8a069ab8a24b50aecff325dcd22ffa
context deadline exceeded
blesh > panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x4009369]

goroutine 41 [running]:
panic(0x41df820, 0xc420014080)
    /usr/local/Cellar/go/1.7.3/libexec/src/runtime/panic.go:500 +0x1a1
main.cmdConnect.func1(0x0, 0x0)
    /Users/.../Documents/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:261 +0x29
created by main.cmdConnect
    /Users/.../Documents/Source/src/github.com/currantlabs/ble/examples/blesh/main.go:265 +0x2b7
~/D/S/s/g/c/ble> ./blesh sh
Initializing device ...
blesh > connect -a ee8a069ab8a24b50aecff325dcd22ffa
Dialing to specified address: ee8a069ab8a24b50aecff325dcd22ffa
blesh > explore
Discovering profile...
    Service: 47fe55d8447f43ef9ad9fe6325e17c47 , Handle (0x10)
      Characteristic: 686a9a3b4c2c4231b8719cfe92cc6b1e , Property: 0x12 (NR), Handle(0x11), VHandle(0x12)
        Value         f314e4e2dd0dfe0c000007000000000017011100 | "\xf3\x14\xe4\xe2\xdd\r\xfe\f\x00\x00\a\x00\x00\x00\x00\x00\x17\x01\x11\x00"
        Descriptor: 2902 Client Characteristic Configuration, Handle(0x13)
        Value         0000 | "\x00\x00"
      Characteristic: 078ff5d63c9347f5a30c05563b8d831e , Property: 0x12 (RN), Handle(0x14), VHandle(0x15)
        Value         810302 | "\x81\x03\x02"
        Descriptor: 2902 Client Characteristic Configuration, Handle(0x16)
        Value         0000 | "\x00\x00"
      Characteristic: b962bdd15a77479793a1ede8d0ff74bd , Property: 0x0C (wW), Handle(0x17), VHandle(0x18)
roylee17 commented 7 years ago

Is the attached hcidump a mix of multiple sessions?

Surface Mouse looks like a connection problem while sensor module could be a L2CAP issue (at least it connects, and errored out on explorer). Let's see if we can isolate them.

Can you run two hcidump for the surface mouse. One with explorer of paypal/gatt and the other with the scanner of currantlabs/ble?

And the same with the sensor module.

Do you have other Linux platform that can run the scanner against those to peripherals and succeed? That will be even better for a side by side reference for RPi.

blin4444 commented 7 years ago

I have temporarily stopped working on the Bluetooth LE-related project at work. If you are still interested, I could send the dumps in a few weeks.

roylee17 commented 7 years ago

No worries. For the connection problem (other than the L2CAP one), I have a feeling that might be duplicate to #33. So check it out when you come back later.