Need to reverse engineering bluetooth usb mouse dongle, and use mouse board to measure distance, and plot them with matplotlib

I've found these tools that can help with this task:
Linux
- lsusb
- usbmon
- usbhid-dump
- rfcomm
- hidrd-convert
- xxd
- hcitool
- evdev-dump
Windows
Bibliography:
- https://www.silabs.com/Support
Documents/TechnicalDocs/AN249.pdf
- www.usb.org/developers/devclass_docs/HID1_11.pdf
- Usbhid-dump - digimend
- Linux, HID and PyUSB
- MagTek Credit Card Reader in Linux
- Evdev-dump - digimend
Examples:
lnx#> lsusb -s 2
Bus 005 Device 002: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)
Bus 003 Device 002: ID 192f:0716 Avago Technologies, Pte.
Bus 002 Device 002: ID 1bcf:053a Sunplus Innovation Technology Inc. Targa Silvercrest OMC807-C optische Funkmaus
lnx#> sudo lsusb -s 2:4 -v
Bus 002 Device 004: ID 1bcf:053a Sunplus Innovation Technology Inc. Targa Silvercrest OMC807-C optische Funkmaus
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 8
idVendor 0x1bcf Sunplus Innovation Technology Inc.
idProduct 0x053a Targa Silvercrest OMC807-C optische Funkmaus
bcdDevice a0.31
iManufacturer 0
iProduct 2 2.4GHz 2way RF Receiver
iSerial 0
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 34
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 46mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 3 Human Interface Device
bInterfaceSubClass 1 Boot Interface Subclass
bInterfaceProtocol 2 Mouse
iInterface 0
HID Device Descriptor:
bLength 9
bDescriptorType 33
bcdHID 1.00
bCountryCode 0 Not supported
bNumDescriptors 1
bDescriptorType 34 Report
wDescriptorLength 179
Report Descriptors:
** UNAVAILABLE **
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0008 1x 8 bytes
bInterval 10
Device Status: 0x0000
(Bus Powered)
lnx#> lsusb -t
/: Bus 05.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
|__ Port 2: Dev 2, If 0, Class=Wireless, Driver=btusb, 12M
|__ Port 2: Dev 2, If 1, Class=Wireless, Driver=btusb, 12M
/: Bus 04.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/: Bus 03.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
|__ Port 2: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
|__ Port 1: Dev 4, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
|__ Port 2: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M
|__ Port 2: Dev 3, If 1, Class=Human Interface Device, Driver=usbhid, 12M
|__ Port 2: Dev 3, If 2, Class=Human Interface Device, Driver=usbhid, 12M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/8p, 480M
|__ Port 3: Dev 4, If 0, Class=Video, Driver=uvcvideo, 480M
|__ Port 3: Dev 4, If 1, Class=Video, Driver=uvcvideo, 480M
|__ Port 3: Dev 4, If 2, Class=Audio, Driver=snd-usb-audio, 480M
|__ Port 3: Dev 4, If 3, Class=Audio, Driver=snd-usb-audio, 480M
lnx#> sudo usbmon -f0
ffff8800764640c0 4022916298 C Ii:003:01 0 8 = 00000000 00000000
ffff8800764640c0 4022916321 S Ii:003:01 -115 8 <
ffff880076464a80 4025994204 C Ii:004:01 0 6 = 0100ff00 0000
ffff880076464a80 4025994229 S Ii:004:01 -115 7 <
ffff880076464a80 4026010207 C Ii:004:01 0 6 = 0100ff00 0000
ffff880076464a80 4026010238 S Ii:004:01 -115 7 <
ffff880076464a80 4026018204 C Ii:004:01 0 6 = 0100ff00 0000
ffff880076464a80 4026018225 S Ii:004:01 -115 7 <
ffff880076464a80 4026026199 C Ii:004:01 0 6 = 0100ff00 0000
lnx#> sudo usbhid-dump -s 2:4
002:004:001:DESCRIPTOR 1393614919.941888
05 01 09 02 A1 01 85 01 05 09 19 01 29 08 15 00
25 01 95 08 75 01 81 02 05 01 09 01 A1 00 09 30
09 31 09 38 15 81 25 7F 75 08 95 03 81 06 05 0C
0A 38 02 95 01 81 06 C0 06 00 FF 09 01 15 00 26
FF 00 75 08 95 07 B1 02 C0 06 F3 F1 0A F3 F1 A1
01 85 02 09 00 95 01 75 08 15 00 26 FF 00 81 02
C0 05 0C 09 01 A1 01 85 03 15 00 26 FF 03 19 00
2A FF 03 75 10 95 03 81 00 C0 05 01 09 80 A1 01
85 04 05 01 19 81 29 83 15 00 25 01 95 03 75 01
81 06 95 01 75 05 81 01 C0 06 BC FF 09 88 A1 01
85 05 19 00 2A FF 00 15 00 26 FF 00 75 08 95 01
81 00 C0
lnx#> sudo usbhid-dump -s 2:4 | tail -n +2 | xxd -r -p | hidrd-convert -o spec
Usage Page (Desktop), ; Generic desktop controls (01h)
Usage (Mouse), ; Mouse (02h, application collection)
Collection (Application),
Report ID (1),
Usage Page (Button), ; Button (09h)
Usage Minimum (01h),
Usage Maximum (08h),
Logical Minimum (0),
Logical Maximum (1),
Report Count (8),
Report Size (1),
Input (Variable),
Usage Page (Desktop), ; Generic desktop controls (01h)
Usage (Pointer), ; Pointer (01h, physical collection)
Collection (Physical),
Usage (X), ; X (30h, dynamic value)
Usage (Y), ; Y (31h, dynamic value)
Usage (Wheel), ; Wheel (38h, dynamic value)
Logical Minimum (-127),
Logical Maximum (127),
Report Size (8),
Report Count (3),
Input (Variable, Relative),
Usage Page (Consumer), ; Consumer (0Ch)
Usage (AC Pan), ; AC pan (0238h, linear control)
Report Count (1),
Input (Variable, Relative),
End Collection,
Usage Page (FF00h), ; FF00h, vendor-defined
Usage (01h),
Logical Minimum (0),
Logical Maximum (255),
Report Size (8),
Report Count (7),
Feature (Variable),
End Collection,
Usage Page (F1F3h), ; F1F3h, reserved
Usage (F1F3h),
Collection (Application),
Report ID (2),
Usage (00h),
Report Count (1),
Report Size (8),
Logical Minimum (0),
Logical Maximum (255),
Input (Variable),
End Collection,
Usage Page (Consumer), ; Consumer (0Ch)
Usage (Consumer Control), ; Consumer control (01h, application collection)
Collection (Application),
Report ID (3),
Logical Minimum (0),
Logical Maximum (1023),
Usage Minimum (00h),
Usage Maximum (03FFh),
Report Size (16),
Report Count (3),
Input,
End Collection,
Usage Page (Desktop), ; Generic desktop controls (01h)
Usage (Sys Control), ; System control (80h, application collection)
Collection (Application),
Report ID (4),
Usage Page (Desktop), ; Generic desktop controls (01h)
Usage Minimum (Sys Power Down), ; System power down (81h, one-shot control)
Usage Maximum (Sys Wake Up), ; System wake up (83h, one-shot control)
Logical Minimum (0),
Logical Maximum (1),
Report Count (3),
Report Size (1),
Input (Variable, Relative),
Report Count (1),
Report Size (5),
Input (Constant),
End Collection,
Usage Page (FFBCh), ; FFBCh, vendor-defined
Usage (88h),
Collection (Application),
Report ID (5),
Usage Minimum (00h),
Usage Maximum (FFh),
Logical Minimum (0),
Logical Maximum (255),
Report Size (8),
Report Count (1),
Input,
End Collection
lnx#> sudo usbhid-dump -s 2:4 -es
Starting dumping interrupt transfer stream
with 1 minute timeout.
002:004:001:STREAM 1393615361.032412
01 00 01 FF 00 00
002:004:001:STREAM 1393615361.392401
01 00 FF 00 00 00
002:004:001:STREAM 1393615361.400380
01 00 FC 02 00 00
002:004:001:STREAM 1393615361.424382
01 00 FF 01 00 00
002:004:001:STREAM 1393615361.432377
01 00 FE 00 00 00
002:004:001:STREAM 1393615361.440378
01 00 FF 01 00 00
002:004:001:STREAM 1393615361.592374
01 00 FF 00 00 00
002:004:001:STREAM 1393615361.608392
01 00 FF 00 00 00
002:004:001:STREAM 1393615361.616373
01 00 FF 00 00 00
Source: (StackOverflow)
I'm trying to output a string from a barcode or qrcode using a Honeywell USB 3310g scanner in Ubuntu. I have libusb and a library called metro-usb (http://gitorious.org/other/metro-usb) which are enabling my scanner to work. If i open gedit and scan, I successfully get the qrcode data fine! I need to be able to build this pragmatically and send the data to a web service.
I've downloaded and instally pyusb and this seems to be the best solution best on my research. I have a script based on things i've found on the web that waits for the scan and successfully gives me a result on the scan even. however, I am unsure how to read the data. I get an array of data that doesn't really make any sense. I'm not familiar with USB programming or Python either. Can anyone direct me to the best way to simply get the string of data from the USB barcode scan? I can most-likely figure out the rest. Also, I am getting an error that I always have to comment out to get the program to run when I try to set_configuration. I always get a Error saying Resource is Busy...not sure if this has anything to do with it..I've gotten this script off the web, and the Product ID and Vendor ID from my lsusb command. When i run this with the set_configuration commented out, I get:
Waiting to read...
When scanned:
0x0 0x0 0xb 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x17 0x0 0x0 0x0 0x0 0x0
Traceback (most recent call last):
File "reader.py", line 81, in <module>
print lecture_code_barre()
File "reader.py", line 76, in lecture_code_barre
lecture+=NO_SCAN_CODE[data[n+2]]
KeyError: 11
Here is the py script:
import sys
import usb.core
import usb.util
def lecture_code_barre():
VENDOR_ID = 0x0c2e
PRODUCT_ID = 0x0b61
#VENDOR_ID = 0x04b4
#PRODUCT_ID = 0x0100
DATA_SIZE = 16 # 224
NO_SCAN_CODE = {0x1E:'1', 0x1F:'2', 0x20:'3', 0x21:'4', 0x22:'5', 0x23:'6', 0x24:'7'
, 0x25:'8', 0x26:'9', 0x27:'0', 0x28:''} # 28=enter
device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
if device is None:
sys.exit("Could not find Id System Barcode Reader.")
if device.is_kernel_driver_active(0): # On détache le périphérique du kernel, plus d'envoi sur stdin
try:
device.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit("Could not detatch kernel driver: %s" % str(e))
#try:
# device.set_configuration()
# device.reset()
#except usb.core.USBError as e:
# sys.exit("Could not set configuration: %s" % str(e))
endpoint = device[0][(0,0)][0]
data = []
lu = False
print "Waiting to read..."
lecture=''
while 1:
try:
data += device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
if not lu:
print "Waiting to read..."
lu = True
except usb.core.USBError as e:
if e.args == (110,'Operation timed out') and lu:
if len(data) < DATA_SIZE:
print "Lecture incorrecte, recommencez. (%d bytes)" % len(data)
print "Data: %s" % ''.join(map(hex, data))
data = []
lu = False
continue
else:
for n in range(0,len(data),16):
print ' '.join(map(hex,data[n:n+16]))
lecture+=NO_SCAN_CODE[data[n+2]]
break # Code lu
return lecture
if __name__ == '__main__':
print lecture_code_barre()
Output from device with looping:
....
print device.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize)
...
parallels@ubuntu:~/Documents/pyusb$ sudo python scan.py
<usb.core.Device object at 0x7fa5f34df610>
array('B', [0, 0, 38, 0, 0, 0, 0, 0])
array('B', [2, 0, 0, 0, 0, 0, 0, 0])
array('B', [2, 0, 11, 0, 0, 0, 0, 0])
array('B', [2, 0, 22, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 81, 0, 0, 0, 0, 0])
array('B', [2, 0, 0, 0, 0, 0, 0, 0])
array('B', [2, 0, 24, 0, 0, 0, 0, 0])
array('B', [2, 0, 14, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 81, 0, 0, 0, 0, 0])
array('B', [0, 0, 11, 0, 0, 0, 0, 0])
array('B', [0, 0, 23, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 23, 0, 0, 0, 0, 0])
array('B', [0, 0, 19, 0, 0, 0, 0, 0])
array('B', [2, 0, 0, 0, 0, 0, 0, 0])
array('B', [2, 0, 51, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 56, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 56, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 55, 0, 0, 0, 0, 0])
array('B', [0, 0, 5, 0, 0, 0, 0, 0])
array('B', [0, 0, 21, 0, 0, 0, 0, 0])
array('B', [0, 0, 18, 0, 0, 0, 0, 0])
array('B', [0, 0, 26, 0, 0, 0, 0, 0])
array('B', [0, 0, 17, 0, 0, 0, 0, 0])
array('B', [0, 0, 5, 0, 0, 0, 0, 0])
array('B', [0, 0, 18, 0, 0, 0, 0, 0])
array('B', [0, 0, 0, 0, 0, 0, 0, 0])
array('B', [0, 0, 18, 0, 0, 0, 0, 0])
array('B', [0, 0, 14, 0, 0, 0, 0, 0])
array('B', [0, 0, 55, 0, 0, 0, 0, 0])
array('B', [0, 0, 17, 0, 0, 0, 0, 0])
array('B', [0, 0, 8, 0, 0, 0, 0, 0])
array('B', [0, 0, 23, 0, 0, 0, 0, 0])
array('B', [0, 0, 81, 0, 0, 0, 0, 0])
array('B', [0, 0, 22, 0, 0, 0, 0, 0])
array('B', [0, 0, 8, 0, 0, 0, 0, 0])
array('B', [0, 0, 21, 0, 0, 0, 0, 0])
array('B', [0, 0, 25, 0, 0, 0, 0, 0])
array('B', [0, 0, 12, 0, 0, 0, 0, 0])
array('B', [0, 0, 6, 0, 0, 0, 0, 0])
array('B', [0, 0, 8, 0, 0, 0, 0, 0])
Source: (StackOverflow)