matmair / brother_ql-inventree

Python package for the raster language protocol of the Brother QL series label printers
GNU General Public License v3.0
27 stars 10 forks source link

P-Touch Editor Lite detection, basic compatibility checks and better error handling for pyusb #65

Closed vulpes2 closed 3 months ago

vulpes2 commented 3 months ago
  1. P-Touch Editor Lite detection:
    • pyusb: check for USB Mass Storage devices with a Brother VID
    • linux_kernel: look for a persistent block device name created by udev, I've avoided pyusb and udisks on purpose to maximize compatibility
  2. Compatibility checks: when running discovery with the linux_kernel backend, parse the IEEE1284 ID from /sys/class/usbmisc/lpX/device/ieee1284_id and filter for Brother printers with the PT-CBT command set. Incompatible printers will generate a message but won't be returned by the function. Every single USB P-Touch printer should be able to pass this check. While it's possible to do this with libusb to make sure it doesn't pick up Brother printers that aren't P-Touch devices, it's pretty complicated, so I didn't do it with libusb yet.
  3. Graceful error handling with libusb. The backend will now clean up after itself when a libusb error occurs and re-attach the kernel driver if required. 0x04f9:0x0330 is a Brother printer but it's not P-Touch. It's used by usbfs that libusb cannot unbind due to the lack of permissions, I'm only using it to demonstrate the improved error handling.
$ brother_ql -b pyusb discover
WARNING:brother_ql.backends.pyusb:Detected USB label printer in the unsupported P-Touch Editor Lite mode: PT-P700
WARNING:brother_ql.backends.pyusb:Disable P-Touch Editor Lite mode by holding down the corresponding button on the printer until the light goes off.
ERROR:brother_ql.backends.pyusb:Cannot initialize device 0x04f9:0x0330/XXXXXXXXXX: [Errno 16] Resource busy
$ brother_ql -b linux_kernel discover
INFO:brother_ql.backends.linux_kernel:Compatible printer at lp0: Brother QL-700
WARNING:brother_ql.backends.linux_kernel:Detected a label printer PT-P700 in the unsupported P-Touch Editor Lite mode.
WARNING:brother_ql.backends.linux_kernel:Disable P-Touch Editor Lite by holding down the corresponding button on the printer until the light goes off.
Found compatible printer QL-700 at: file:///dev/usb/lp0
$ brother_ql -b pyusb discover
WARNING:brother_ql.backends.pyusb:Detected USB label printer in the unsupported P-Touch Editor Lite mode: PT-P700
WARNING:brother_ql.backends.pyusb:Disable P-Touch Editor Lite mode by holding down the corresponding button on the printer until the light goes off.
Found compatible printer QL-700 at: usb://0x04f9:0x2042/XXXXXXXXXX

Fixes https://github.com/matmair/brother_ql-inventree/issues/64

vulpes2 commented 3 months ago

@matmair not sure if you had notifications enabled for this repo or not, just want to make sure you're aware of this.

matmair commented 3 months ago

@vulpes2 I was a bit occupied with inventree/inventree stuff. Will review tomorrow or the weekend

sonarcloud[bot] commented 3 months ago

Quality Gate Passed Quality Gate passed

Issues
2 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

vulpes2 commented 3 months ago

Pushed some minor fixes that aren't worth opening a separate pull request for.