jahnf / Projecteur

Linux Desktop Application for the Logitech Spotlight device (and similar devices) - Digital Laser Pointer
MIT License
379 stars 33 forks source link

Enable Hidraw interface on spotlight bluetooth connection #140

Closed mayanksuman closed 3 years ago

mayanksuman commented 3 years ago

This commit enables hidraw interface on logitech spotlight connected via bluetooth. Three major changes are

  1. DeviceId (in device.h) now include the information about the bus on which spotlight is connected (USB/Bluetooth).
  2. sendData function (device.cc) for hidraw on bluetooth modifies the data before sending it. The bluetooth hid need the data in 20 byte long packages; smaller packet of 7 byte length is not allowed on bluetooth connection. More details for this conversion is provided in the function defintion as comment.
  3. The projecteur now initialize HID device correctly (Get rid of any device configuration by other programs). The projecteur also pings the device and check the the HID++ version supported by the device.

With this code, device can vibrate (and support other functionality as being worked in #136 ) even if it is connected on bluetooth.

Note: To connect logitech spotlight using bluetooth, press top button and the last button till the led light starting flashing. The spotlight device can now be paired with computer.

mayanksuman commented 3 years ago

Important Note: The spotlight device is designed for only one active connection at a time. The Projecteur seem to not work when device is connected by both means (USB and Bluetooth). However, to correct it press any key on spotlight device. The bluetooth connection will automatically disconnect and Projecteur will be able to work again.

jahnf commented 3 years ago

Hi @mayanksuman , thank you for your work 👍 - I am currently traveling this weekend - but will review and integrate via feature branch in the next days.

mayanksuman commented 3 years ago

Hi @jahnf , all the comments have taken care of. I have used QTimer instead of nanosleep.

We are checking the response of ping in async manner. We are not putting a timer with timeout for this case. Rather, the response to all the HID commands are handled in onHIDDataAvailable function in src/spotlight.cc, whenever it is available.

It is not necessary that device send information only on request, sometime like connecting device for charging or other cases, it sends the information on its own. Hence, I thought that instead of checking the response to each HID command, let the HID response come and process it when it is available.

jahnf commented 3 years ago

Hi @mayanksuman

What Linux-Distro are you running? I encounter two problems with this on my Ubuntu 20.04 machines when using bluetooth with the Spotlight device.

As you collected a lot of experience with the device, what could be the reason for this?

Just for information, the Bluetooth device on my machine detected by Projecteur looks like this:

Projecteur 1.0-alpha.33; device scan

 * Found 1 supported devices. (0 readable, 0 writable)

 +++ name:     'SPOTLIGHT'
     userName: 'Logitech Spotlight (Bluetooth)'
     vendorId:  046d
     productId: b503
     phys:      f8:e4:e3:01:27:e5
     busType:   Bluetooth
     devices:   /dev/input/event24, /dev/input/event23, /dev/input/event22, /dev/hidraw3
     readable:  false
     writable:  false
mayanksuman commented 3 years ago

What Linux-Distro are you running?

I am using Debian Testing on my main machine.

In your device scan, some sub-device (most probably /dev/hidraw3) is not readable or writable. So, even if the device is sending a connection active packet ({0x11, 0x01, 0x04, ...}), the software (Projecteur) is not able to receive it and hence reports that the device is not active.

I think, this is a issue with udev rule. One more point, are you running a virtual machine?

I have not tested bluetooth connection on virtual machine, as my machine do not provide mean to pass bluetooth device to virtual machine.

jahnf commented 3 years ago

Nope not a virtual machine and all the sub-devices are readable when running as root. I have the issue even when running as root. As you see in the log, data gets written to hidraw3 and also received, but when sending the vibrate command it results in this:

[2021-07-19T08:02:28.567][dbg][projecteur.HID] Write 1101001d00005d00000000000000000000000000 to /dev/hidraw3
[2021-07-19T08:02:28.567][dbg][projecteur.HID] Write 1101051d00000000000000000000000000000000 to /dev/hidraw3
[2021-07-19T08:02:29.108][dbg][projecteur.HID] Received 1101051d00000000000000000000000000000000 from /dev/hidraw3
[2021-07-19T08:02:48.765][inf][projecteur.HID] The device is not active. Activate it by pressing any button on device.
[2021-07-19T08:02:53.565][inf][projecteur.HID] The device is not active. Activate it by pressing any button on device.

Device scan as root:

Projecteur 1.0-alpha.33; device scan

 * Found 1 supported devices. (1 readable, 1 writable)

 +++ name:     'SPOTLIGHT'
     userName: 'Logitech Spotlight (Bluetooth)'
     vendorId:  046d
     productId: b503
     phys:      f8:e4:e3:01:27:e5
     busType:   Bluetooth
     devices:   /dev/input/event22, /dev/input/event23, /dev/input/event24, /dev/hidraw3
     readable:  true
     writable:  true

Edit: added addtional udev rules, so that as a user all sub-devices are now readable - issue remains the same when running as a regular user. - Don't have too much time now to investigate further, but any idea that you have is appreciated - will investigate more later this week.

mayanksuman commented 3 years ago

Hi @jahnf ,

My mistake. Device activation packet is only sent in case of USB connection. If device is connected on Bluetooth it should be considered online all the time.

Please change the isOnline function definition in device.h to

bool isOnline() { return (m_details.busType == BusType::Bluetooth ||
                            m_details.hidProtocolVer > 0); };

You can directly merge #142 in your feature branch with this fix.

jahnf commented 3 years ago

Thanks, works now - will review the battery PR within the next days.

mayanksuman commented 3 years ago

Ok ... Should I rebase battery PR to your feature/spotlight-hidraw-functionality branch?

jahnf commented 3 years ago

Yes that would be helpful - I'd like to do tests on the feature branch for multiple systems before finally merging to develop.

mayanksuman commented 3 years ago

Ok ... I will rebase and push on that PR.