USB BARCODE READER

Fork of USBHOST by ST

Revision:
5:fc157e6bd5a5
Parent:
1:ab240722d7ef
diff -r 1c76b46ad779 -r fc157e6bd5a5 USBHostHID/USBHostMouse.cpp
--- a/USBHostHID/USBHostMouse.cpp	Fri Feb 17 12:40:57 2017 +0100
+++ b/USBHostHID/USBHostMouse.cpp	Wed Apr 26 18:11:37 2017 +0200
@@ -46,7 +46,8 @@
     return dev_connected;
 }
 
-bool USBHostMouse::connect() {
+bool USBHostMouse::connect()
+{
     int len_listen;
 
     if (dev_connected) {
@@ -58,25 +59,32 @@
 
             if(host->enumerate(dev, this))
                 break;
-
             if (mouse_device_found) {
-
-                int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
-                if (!int_in)
-                    break;
+                {
+                    /* As this is done in a specific thread
+                     * this lock is taken to avoid to process the device
+                     * disconnect in usb process during the device registering */
+                    USBHost::Lock  Lock(host);
+                    int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
+                    if (!int_in)
+                        break;
 
-                USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
-                dev->setName("Mouse", mouse_intf);
-                host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
+                    USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
+                    dev->setName("Mouse", mouse_intf);
+                    host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
 
-                int_in->attach(this, &USBHostMouse::rxHandler);
-                len_listen = int_in->getSize();
-                if (len_listen > sizeof(report)) {
-                    len_listen = sizeof(report);
+                    int_in->attach(this, &USBHostMouse::rxHandler);
+                    len_listen = int_in->getSize();
+                    if (len_listen > sizeof(report)) {
+                        len_listen = sizeof(report);
+                    }
                 }
-                host->interruptRead(dev, int_in, report, len_listen, false);
-
-                dev_connected = true;
+                int ret=host->interruptRead(dev, int_in, report, len_listen, false);
+                MBED_ASSERT((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING) || (ret == USB_TYPE_FREE));
+                if ((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING))
+                    dev_connected = true;
+                if (ret == USB_TYPE_FREE)
+                    dev_connected = false;
                 return true;
             }
         }
@@ -86,33 +94,37 @@
 }
 
 void USBHostMouse::rxHandler() {
-    int len_listen = int_in->getSize();
+    int len_listen = int_in->getLengthTransferred();
+    if (len_listen !=0) {
 
-    if (onUpdate) {
-        (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
-    }
+        if (onUpdate) {
+            (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
+        }
 
-    if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
-        (*onButtonUpdate)(report[0] & 0x07);
-    }
+        if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
+            (*onButtonUpdate)(report[0] & 0x07);
+        }
 
-    if (onXUpdate && (x != report[1])) {
-        (*onXUpdate)(report[1]);
-    }
+        if (onXUpdate && (x != report[1])) {
+            (*onXUpdate)(report[1]);
+        }
 
-    if (onYUpdate && (y != report[2])) {
-        (*onYUpdate)(report[2]);
-    }
+        if (onYUpdate && (y != report[2])) {
+            (*onYUpdate)(report[2]);
+        }
 
-    if (onZUpdate && (z != report[3])) {
-        (*onZUpdate)(report[3]);
+        if (onZUpdate && (z != report[3])) {
+            (*onZUpdate)(report[3]);
+        }
+
+        // update mouse state
+        buttons = report[0] & 0x07;
+        x = report[1];
+        y = report[2];
+        z = report[3];
     }
-
-    // update mouse state
-    buttons = report[0] & 0x07;
-    x = report[1];
-    y = report[2];
-    z = report[3];
+    /*  set again the maximum value */
+    len_listen = int_in->getSize();
 
     if (len_listen > sizeof(report)) {
         len_listen = sizeof(report);