Heerkog / MicroPythonBLEHID

Human Interface Device (HID) over Bluetooth Low Energy (BLE) GATT library for MicroPython.
GNU General Public License v3.0
204 stars 28 forks source link

Keyboard sample, immediatelly disconnects due to an 'Authentication Failure' #15

Closed aguaviva closed 1 year ago

aguaviva commented 1 year ago

I see the keyboard being advertised, I can connect to it but immediately disconnects.

I am using Linux and MicroPython v1.19.1-613-g2d406f222

see the btmon event trace:

 MGMT Event: Device Connected (0x000b) plen 34       {0x0001} [hci0] 45.537264
        LE Address: 80:7D:3A:B9:BA:B2 (Espressif Inc.)
        Flags: 0x00000008
          Connection Locally Initiated
        Data length: 21
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        Name (complete): Keyboard
        16-bit Service UUIDs (complete): 1 entry
          Human Interface Device (0x1812)
        Appearance: Keyboard (0x03c1)
< HCI Command: LE Read Remote Use.. (0x08|0x0016) plen 2  #568 [hci0] 45.537450
        Handle: 3585
> HCI Event: Command Status (0x0f) plen 4                 #569 [hci0] 45.539070
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 11                 #570 [hci0] 45.649114
      LE Data Length Change (0x07)
        Handle: 3585
        Max TX octets: 251
        Max TX time: 2120
        Max RX octets: 27
        Max RX time: 328
> HCI Event: LE Meta Event (0x3e) plen 12                 #571 [hci0] 45.798988
      LE Read Remote Used Features (0x04)
        Status: Success (0x00)
        Handle: 3585
        Features: 0xff 0x00 0x00 0x00 0x00 0x00 0x00 0x00
          LE Encryption
          Connection Parameter Request Procedure
          Extended Reject Indication
          Peripheral-initiated Features Exchange
          LE Ping
          LE Data Packet Length Extension
          LL Privacy
          Extended Scanner Filter Policies
< HCI Command: LE Start Encryption (0x08|0x0019) plen 28  #572 [hci0] 45.799318
        Handle: 3585
        Random number: 0x0000000000000000
        Encrypted diversifier: 0x0000
        Long term key: a8b63eadf0a404551f2abd04420a0007
> HCI Event: Command Status (0x0f) plen 4                 #573 [hci0] 45.800974
      LE Start Encryption (0x08|0x0019) ncmd 1
        Status: Success (0x00)
> HCI Event: Encryption Change (0x08) plen 4              #574 [hci0] 46.048954
        Status: PIN or Key Missing (0x06)
        Handle: 3585
        Encryption: Disabled (0x00)
< HCI Command: Disconnect (0x01|0x0006) plen 3            #575 [hci0] 46.049023
        Handle: 3585
        Reason: Authentication Failure (0x05)
> HCI Event: Command Status (0x0f) plen 4                 #576 [hci0] 46.049924
      Disconnect (0x01|0x0006) ncmd 1
        Status: Success (0x00)
> HCI Event: Inquiry Complete (0x01) plen 1               #577 [hci0] 46.054048
        Status: Success (0x00)
@ MGMT Event: Discovering (0x0013) plen 2             {0x0001} [hci0] 46.054151
        Address type: 0x07
          BR/EDR
          LE Public
          LE Random
        Discovery: Disabled (0x00)
@ MGMT Command: Start Service Disc.. (0x003a) plen 4  {0x0001} [hci0] 46.054377
        Address type: 0x07
          BR/EDR
          LE Public
          LE Random
        RSSI: invalid (0x7f)
        UUIDs: 0
< HCI Command: LE Set Random Addr.. (0x08|0x0005) plen 6  #578 [hci0] 46.054427
        Address: 0B:3A:9C:04:F6:D5 (Non-Resolvable)
> HCI Event: Command Complete (0x0e) plen 4               #579 [hci0] 46.054935
      LE Set Random Address (0x08|0x0005) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Parame.. (0x08|0x000b) plen 7  #580 [hci0] 46.054993
        Type: Active (0x01)
        Interval: 22.500 msec (0x0024)
        Window: 11.250 msec (0x0012)
        Own address type: Random (0x01)
        Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4               #581 [hci0] 46.055952
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2    #582 [hci0] 46.056219
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4               #583 [hci0] 46.058059
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
< HCI Command: Inquiry (0x01|0x0001) plen 5               #584 [hci0] 46.058297
        Access code: 0x9e8b33 (General Inquiry)
        Length: 10.24s (0x08)
        Num responses: 0
> HCI Event: Command Status (0x0f) plen 4                 #585 [hci0] 46.060064
      Inquiry (0x01|0x0001) ncmd 1
        Status: Success (0x00)
@ MGMT Event: Command Complete (0x0001) plen 4        {0x0001} [hci0] 46.060147
      Start Service Discovery (0x003a) plen 1
        Status: Success (0x00)
        Address type: 0x07
          BR/EDR
          LE Public
          LE Random
@ MGMT Event: Discovering (0x0013) plen 2             {0x0001} [hci0] 46.060169
        Address type: 0x07
          BR/EDR
          LE Public
          LE Random
        Discovery: Enabled (0x01)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2    #586 [hci0] 46.066298
        Scanning: Disabled (0x00)
        Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4               #587 [hci0] 46.070057
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
@ MGMT Event: Discovering (0x0013) plen 2             {0x0001} [hci0] 46.070224
        Address type: 0x07
          BR/EDR
          LE Public
          LE Random
        Discovery: Disabled (0x00)
@ MGMT Command: Start Service Disc.. (0x003a) plen 4  {0x0001} [hci0] 46.070387
        Address type: 0x07
          BR/EDR
          LE Public
          LE Random
        RSSI: invalid (0x7f)
        UUIDs: 0
< HCI Command: LE Set Random Addr.. (0x08|0x0005) plen 6  #588 [hci0] 46.070416
        Address: 20:0A:3D:75:7E:B1 (Non-Resolvable)
> HCI Event: Command Complete (0x0e) plen 4               #589 [hci0] 46.071942
      LE Set Random Address (0x08|0x0005) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Parame.. (0x08|0x000b) plen 7  #590 [hci0] 46.072136
        Type: Active (0x01)
        Interval: 22.500 msec (0x0024)
        Window: 11.250 msec (0x0012)
        Own address type: Random (0x01)
        Filter policy: Accept all advertisement (0x00)
> HCI Event: Command Complete (0x0e) plen 4               #591 [hci0] 46.072924
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2    #592 [hci0] 46.072952
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4               #593 [hci0] 46.074956
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
< HCI Command: Inquiry (0x01|0x0001) plen 5               #594 [hci0] 46.075132
        Access code: 0x9e8b33 (General Inquiry)
        Length: 10.24s (0x08)
        Num responses: 0
> HCI Event: Command Status (0x0f) plen 4                 #595 [hci0] 46.075921
      Inquiry (0x01|0x0001) ncmd 1
        Status: Command Disallowed (0x0c)
@ MGMT Event: Command Complete (0x0001) plen 4        {0x0001} [hci0] 46.075959
      Start Service Discovery (0x003a) plen 1
        Status: Busy (0x0a)
        Address type: 0x07
          BR/EDR
          LE Public
          LE Random
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2    #596 [hci0] 46.075984
        Scanning: Disabled (0x00)
        Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4               #597 [hci0] 46.087952
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: Disconnect Complete (0x05) plen 4            #598 [hci0] 46.098954
        Status: Success (0x00)
        Handle: 3585
        Reason: Connection Terminated By Local Host (0x16)
@ MGMT Event: Device Disconnected (0x000c) plen 8     {0x0001} [hci0] 46.099112
        LE Address: 80:7D:3A:B9:BA:B2 (Espressif Inc.)
        Reason: Connection terminated due to authentication failure (0x04)
= bluetoothd: profiles/deviceinfo/deviceinfo.c:read_pnpid_cb() Er..   46.099666
= bluetoothd: profiles/deviceinfo/dis.c:read_pnpid_cb() Error rea..   46.099689
= bluetoothd: profiles/input/hog-lib.c:info_read_cb() HID Informa..   46.099706
= bluetoothd: profiles/input/hog-lib.c:report_reference_cb() Read..   46.099728
= bluetoothd: profiles/input/hog-lib.c:report_reference_cb() Read..   46.099750
= bluetoothd: profiles/input/hog-lib.c:proto_mode_read_cb() Proto..   46.099766
< HCI Command: LE Set Scan Parame.. (0x08|0x000b) plen 7  #599 [hci0] 46.150267
        Type: Passive (0x00)
        Interval: 60.000 msec (0x0060)
        Window: 30.000 msec (0x0030)
        Own address type: Public (0x00)
        Filter policy: Ignore not in accept list (0x01)

See the pic below for more details:

image

Thanks for sharing this lib, looks very well written.

Heerkog commented 1 year ago

Could you try without secrets?

Immediate disconnection often happens when the central (i.e., Linux) is paired with the device and the device is power cycled or the HID services are restarted. Make sure to remove the device from the central's Bluetooth devices before attempting a connection.

Note that there also seem to be several issues with Linux not recognizing notifications because, I suspect, the device information characteristics do not match with actual known devices. I'm unaware of a fix for this.

Please keep me updated.

aguaviva commented 1 year ago

Removing the paired device and adding it again fixed it. Linux doesn't depend on the information characteristics matching any known devices, it is agnostic to that and it only expects devices to follow the protocol.

The problem now is that I get out of mem errors after a while

image

this is how the main loop of keyboard_example.py looks like, the rest is the same.

if __name__ == "__main__":
    d = Device()
    d.advertise()
    while True:
        d.test()
        print("done")
        time.sleep_ms(2000)
Heerkog commented 1 year ago

Removing the paired device and adding it again fixed it. Linux doesn't depend on the information characteristics matching any known devices, it is agnostic to that and it only expects devices to follow the protocol.

That is indeed how it should work! Yet, I am getting reports of certain distributions not accepting notifications. I'm glad it works for you.

The problem now is that I get out of mem errors after a while

In general this happens with boards with lower amounts of memory, or when notifications follow eachother too quickly. I'd try adding a bit more sleep time/await time in the loops.