Repository: pwr-Solaar/Solaar

Issue:
Device pairing throws IndexError after successful pairing
**Information**
<!-- Do not bother opening an issue for a version older than 1.0.1.  Please update to the latest version and see if your issue persists. -->
- Solaar version (`solaar --version` or `git describe --tags` if cloned from this repository): 1.0.3+dfsg-2
- Distribution: Debian 11
- Kernel version (ex. `uname -srmo`): Linux 5.8.0-1-amd64 x86_64 GNU/Linux
- Output of `solaar show`:
```
Unifying Receiver
  Device path  : /dev/hidraw4
  USB id       : 046d:c52b
  Serial       : 5B9B7DF0
    Firmware   : 24.07.B0030
    Bootloader : 02.09
    Other      : AA.AC
  Has 1 paired device(s) out of a maximum of 6.
  Notifications: wireless, software present (0x000900)
  Device activity counters: 1=182

  1: Wireless Mouse M510
     Codename     : M510v2
     Kind         : mouse
     Wireless PID : 4051
     Protocol     : HID++ 4.5
     Polling rate : 8 ms (125Hz)
     Serial number: 5B9B7DF0
          Firmware: RQM 62.00.B0013
     The power switch is located on the base.
     Supports 22 HID++ 2.0 features:
         0: ROOT                   {0000}   
         1: FEATURE SET            {0001}   
         2: DEVICE FW VERSION      {0003}   
         3: DEVICE NAME            {0005}   
         4: RESET                  {0020}   
         5: BATTERY STATUS         {1000}   
         6: DEVICE RESET           {1802}   internal, hidden
         7: unknown:1810           {1810}   internal, hidden
         8: unknown:1830           {1830}   internal, hidden
         9: unknown:1862           {1862}   internal, hidden
        10: unknown:1890           {1890}   internal, hidden
        11: unknown:18A0           {18A0}   internal, hidden
        12: unknown:18B1           {18B1}   internal, hidden
        13: REPROG CONTROLS V4     {1B04}   
            Actions: {'80': 80, '81': 81, '82': 82, '91': 91, '93': 93, '83': 83, '86': 86}
        14: WIRELESS DEVICE STATUS {1D4B}   
        15: REMAINING PAIRING      {1DF0}   hidden
        16: unknown:1DF3           {1DF3}   internal, hidden
        17: unknown:1E00           {1E00}   hidden
        18: unknown:1EB0           {1EB0}   internal, hidden
        19: unknown:1F03           {1F03}   internal, hidden
        20: LOWRES WHEEL           {2130}   
            Wheel Reports: HID
            HID++ Scrolling: False
        21: POINTER SPEED          {2205}   
            Pointer Speed: 1.0
            Sensitivity (Pointer Speed): 256
     Has 7 reprogrammable keys:
         0: Left Button               , default: Left Click                  => Left Button               
             mse, divertable, pos:0, group:1, gmask:1
         1: Right Button              , default: Right Click                 => Right Button              
             mse, divertable, pos:0, group:1, gmask:1
         2: Middle Button             , default: Mouse Middle Button         => Middle Button             
             mse, reprogrammable, divertable, pos:0, group:2, gmask:3
         3: Left Tilt                 , default: Mouse Scroll Left Button    => Left Tilt                 
             mse, reprogrammable, divertable, pos:0, group:2, gmask:3
         4: Right Tilt                , default: Mouse Scroll Right Button   => Right Tilt                
             mse, reprogrammable, divertable, pos:0, group:2, gmask:3
         5: Back Button               , default: Mouse Back Button           => Back Button               
             mse, reprogrammable, divertable, pos:0, group:2, gmask:3
         6: Forward Button            , default: Mouse Forward Button        => Forward Button            
             mse, reprogrammable, divertable, pos:0, group:2, gmask:3
     Battery: 70%, discharging, next level 5%.
```

**Describe the bug**
In my setup I share a Logitech mouse between two computers, each with their own Unifying receiver. Consequently, I have to pair the mouse each time I want to use the other system. The initial pairing is always successful but throws an exception:
```
solaar: error: Traceback (most recent call last):
  File "/usr/share/solaar/lib/solaar/cli/__init__.py", line 175, in run
    m.run(c, args, _find_receiver, _find_device)
  File "/usr/share/solaar/lib/solaar/cli/pair.py", line 79, in run
    receiver.handle.notifications_hook(n)
  File "/usr/share/solaar/lib/solaar/cli/pair.py", line 59, in notifications_hook
    receiver.status.new_device = receiver[n.devnumber]
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 519, in __getitem__
    raise IndexError(key)
IndexError: 0
```

**To Reproduce**
Steps to reproduce the behavior:
1. Pair mouse with a secondary receiver on a different computer
2. Unpair mouse with the secondary receiver
3. Pair mouse with primary receiver on the intended computer using `solaar pair`
4. See error

**Additional context**
Here is the debug output from pairing:
```
$ solaar -ddd pair
09:33:05,868    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 030000]
09:33:05,870    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 FF 83B5 035B9B7DF002063E0000000000000000]
09:33:05,870    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 80B2 000000]
09:33:05,872    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4101 5140040000000400000000]
09:33:05,874    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 00 4102 0000000000000000000000]
09:33:05,876    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F80 B20300]
09:33:05,876    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {80B2}: 3 = invalid value
09:33:05,876    DEBUG [MainThread] solaar.cli: [/dev/hidraw4] => <UnifyingReceiver(/dev/hidraw4,3)>
09:33:05,937    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 8100 000000]
09:33:05,940    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8100 000900]
09:33:05,940    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 200000]
09:33:05,942    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 FF 83B5 203E0840510402020C00000000000000]
09:33:05,942     INFO [MainThread] logitech_receiver.receiver: <UnifyingReceiver(/dev/hidraw4,3)>: found new device 1 (4051)
09:33:05,942    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 210000]
09:33:05,944    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,944    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,944    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 040000]
09:33:05,946    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,946    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,946    ERROR [MainThread] logitech_receiver.receiver: failed to read Nano wpid for device 2 of <UnifyingReceiver(/dev/hidraw4,3)>
09:33:05,946    ERROR [MainThread] logitech_receiver.receiver: register_new_device
Traceback (most recent call last):
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 465, in register_new_device
    dev = PairedDevice(self, number, notification)
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 127, in __init__
    raise _base.NoSuchDevice(number=number, receiver=receiver, error='read Nano wpid')
logitech_receiver.base.NoSuchDevice: {'number': 2, 'receiver': <UnifyingReceiver(/dev/hidraw4,3)>, 'error': 'read Nano wpid'}
09:33:05,947  WARNING [MainThread] logitech_receiver.receiver: <UnifyingReceiver(/dev/hidraw4,3)>: looked for device 2, not found
09:33:05,947    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 220000]
09:33:05,950    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,950    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,950    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 040000]
09:33:05,952    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,952    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,952    ERROR [MainThread] logitech_receiver.receiver: failed to read Nano wpid for device 3 of <UnifyingReceiver(/dev/hidraw4,3)>
09:33:05,952    ERROR [MainThread] logitech_receiver.receiver: register_new_device
Traceback (most recent call last):
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 465, in register_new_device
    dev = PairedDevice(self, number, notification)
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 127, in __init__
    raise _base.NoSuchDevice(number=number, receiver=receiver, error='read Nano wpid')
logitech_receiver.base.NoSuchDevice: {'number': 3, 'receiver': <UnifyingReceiver(/dev/hidraw4,3)>, 'error': 'read Nano wpid'}
09:33:05,952  WARNING [MainThread] logitech_receiver.receiver: <UnifyingReceiver(/dev/hidraw4,3)>: looked for device 3, not found
09:33:05,953    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 230000]
09:33:05,954    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,954    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,954    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 040000]
09:33:05,956    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,956    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,956    ERROR [MainThread] logitech_receiver.receiver: failed to read Nano wpid for device 4 of <UnifyingReceiver(/dev/hidraw4,3)>
09:33:05,956    ERROR [MainThread] logitech_receiver.receiver: register_new_device
Traceback (most recent call last):
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 465, in register_new_device
    dev = PairedDevice(self, number, notification)
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 127, in __init__
    raise _base.NoSuchDevice(number=number, receiver=receiver, error='read Nano wpid')
logitech_receiver.base.NoSuchDevice: {'number': 4, 'receiver': <UnifyingReceiver(/dev/hidraw4,3)>, 'error': 'read Nano wpid'}
09:33:05,956  WARNING [MainThread] logitech_receiver.receiver: <UnifyingReceiver(/dev/hidraw4,3)>: looked for device 4, not found
09:33:05,956    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 240000]
09:33:05,958    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,958    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,958    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 040000]
09:33:05,960    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,960    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,960    ERROR [MainThread] logitech_receiver.receiver: failed to read Nano wpid for device 5 of <UnifyingReceiver(/dev/hidraw4,3)>
09:33:05,960    ERROR [MainThread] logitech_receiver.receiver: register_new_device
Traceback (most recent call last):
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 465, in register_new_device
    dev = PairedDevice(self, number, notification)
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 127, in __init__
    raise _base.NoSuchDevice(number=number, receiver=receiver, error='read Nano wpid')
logitech_receiver.base.NoSuchDevice: {'number': 5, 'receiver': <UnifyingReceiver(/dev/hidraw4,3)>, 'error': 'read Nano wpid'}
09:33:05,960  WARNING [MainThread] logitech_receiver.receiver: <UnifyingReceiver(/dev/hidraw4,3)>: looked for device 5, not found
09:33:05,960    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 250000]
09:33:05,962    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,962    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,962    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 83B5 040000]
09:33:05,964    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 8F83 B50300]
09:33:05,964    DEBUG [MainThread] logitech_receiver.base: (3) device 0xFF error on request {83B5}: 3 = invalid value
09:33:05,964    ERROR [MainThread] logitech_receiver.receiver: failed to read Nano wpid for device 6 of <UnifyingReceiver(/dev/hidraw4,3)>
09:33:05,964    ERROR [MainThread] logitech_receiver.receiver: register_new_device
Traceback (most recent call last):
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 465, in register_new_device
    dev = PairedDevice(self, number, notification)
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 127, in __init__
    raise _base.NoSuchDevice(number=number, receiver=receiver, error='read Nano wpid')
logitech_receiver.base.NoSuchDevice: {'number': 6, 'receiver': <UnifyingReceiver(/dev/hidraw4,3)>, 'error': 'read Nano wpid'}
09:33:05,964  WARNING [MainThread] logitech_receiver.receiver: <UnifyingReceiver(/dev/hidraw4,3)>: looked for device 6, not found
09:33:05,964    DEBUG [MainThread] logitech_receiver.base: (3) <= w[10 FF 80B2 010014]
09:33:05,966    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 4A01 000000]
09:33:05,966     INFO [MainThread] logitech_receiver.notifications: <UnifyingReceiver(/dev/hidraw4,3)>: pairing lock is open
09:33:05,968    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 80B2 000000]
09:33:09,143    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 01 4002 000000]
09:33:09,145    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 01 4104 525140]
09:33:09,147    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4201 0000000000000000000000]
09:33:09,149    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4000 0000000000000000000000]
09:33:09,151    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4100 5140040000000400000000]
09:33:09,153    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 FF 4A00 000051]
09:33:09,153     INFO [MainThread] logitech_receiver.notifications: <UnifyingReceiver(/dev/hidraw4,3)>: pairing lock is closed
09:33:09,167    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 01 4104 925140]
09:33:09,169    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4200 0000000000000000000000]
09:33:09,171    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0E00 01010100000000000000000000000000]
09:33:09,172    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 00DF7F020000DE00000000]
09:33:09,181    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0004F00000004A00000000]
09:33:09,189    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 000BD00000006300000000]
09:33:09,191    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 FF 83B5 305B9B7DF00400000001000000000000]
09:33:09,193    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 FF 83B5 40044D35313000000000000000000000]
09:33:09,197    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0011B00000007D00000000]
09:33:09,205    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0011 04055A00000000000000000000000000]
09:33:09,211    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 002B30010000E200000000]
09:33:09,219    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0001 0E000000000000000000000000000000]
09:33:09,227    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0032D00000003C00000000]
09:33:09,235    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0001 03000000000000000000000000000000]
09:33:09,243    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0035A00000006900000000]
09:33:09,251    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0301 13000000000000000000000000000000]
09:33:09,259    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 004220000000DC00000000]
09:33:09,265    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0311 576972656C657373204D6F757365204D]
09:33:09,273    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 004EB0FF00004100000000]
09:33:09,281    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0311 35313000000000000000000000000000]
09:33:09,289    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 005B00FF0000E400000000]
09:33:09,297    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0001 00000000000000000000000000000000]
09:33:09,305    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 005D70FE00007300000000]
09:33:09,313    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0001 05000000000000000000000000000000]
09:33:09,319    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0055E0FD00000C00000000]
09:33:09,327    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0500 46050000000000000000000000000000]
09:33:09,335    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0043C0FD00003E00000000]
09:33:09,343    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0510 0200E002020000000000000000000000]
09:33:09,351    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0029B0FD00006800000000]
09:33:09,359    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0500 46050000000000000000000000000000]
09:33:09,367    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 0200 0011C0FD00007000000000]
09:33:09,375    DEBUG [MainThread] logitech_receiver.base: (3) => r[11 01 0510 0200E002020000000000000000000000]
09:33:09,379    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4101 5140040000000400000000]
09:33:09,381    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4101 5140040000000400000000]
09:33:09,383    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 01 4101 5140040000000400000000]
09:33:09,385    DEBUG [MainThread] logitech_receiver.base: (3) => r[20 00 4102 0000000000000000000000]
solaar: error: Traceback (most recent call last):
  File "/usr/share/solaar/lib/solaar/cli/__init__.py", line 175, in run
    m.run(c, args, _find_receiver, _find_device)
  File "/usr/share/solaar/lib/solaar/cli/pair.py", line 79, in run
    receiver.handle.notifications_hook(n)
  File "/usr/share/solaar/lib/solaar/cli/pair.py", line 59, in notifications_hook
    receiver.status.new_device = receiver[n.devnumber]
  File "/usr/share/solaar/lib/logitech_receiver/receiver.py", line 519, in __getitem__
    raise IndexError(key)
IndexError: 0
```


Discussion:

Resolving patch:
diff --git a/lib/solaar/cli/pair.py b/lib/solaar/cli/pair.py
--- a/lib/solaar/cli/pair.py
+++ b/lib/solaar/cli/pair.py
@@ -55,7 +55,7 @@ def notifications_hook(self, n):
             assert n
             if n.devnumber == 0xFF:
                 _notifications.process(receiver, n)
-            elif n.sub_id == 0x41:  # allow for other protocols! (was and n.address == 0x04)
+            elif n.sub_id == 0x41 and len(n.data) == _base._SHORT_MESSAGE_SIZE - 4:
                 kd, known_devices = known_devices, None  # only process one connection notification
                 if kd is not None:
                     if n.devnumber not in kd:


Test patch: