Mike R / USBDevice

Dependents:   frdm_Slider_Keyboard idd_hw2_figlax_PanType idd_hw2_appachu_finger_chording idd_hw3_AngieWangAntonioDeLimaFernandesDanielLim_BladeSymphony ... more

Fork of USBDevice by mbed official

Files at this revision

API Documentation at this revision

Comitter:
mjr
Date:
Fri Mar 17 22:01:47 2017 +0000
Parent:
53:c8110529c24b
Commit message:
Comments

Changed in this revision

USBDevice/USBDevice.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/USBDevice/USBDevice.cpp	Thu Jun 02 18:58:24 2016 +0000
+++ b/USBDevice/USBDevice.cpp	Fri Mar 17 22:01:47 2017 +0000
@@ -80,19 +80,19 @@
 
 bool USBDevice::requestGetDescriptor(void)
 {
+    const uint8_t *p;
     bool success = false;
     printd("get descr: type: %d\r\n", DESCRIPTOR_TYPE(transfer.setup.wValue));
     switch (DESCRIPTOR_TYPE(transfer.setup.wValue))
     {
     case DEVICE_DESCRIPTOR:
-        if (deviceDesc() != NULL)
+        if ((p = deviceDesc()) != NULL)
         {
-            if ((deviceDesc()[0] == DEVICE_DESCRIPTOR_LENGTH) \
-                && (deviceDesc()[1] == DEVICE_DESCRIPTOR))
+            if (p[0] == DEVICE_DESCRIPTOR_LENGTH && p[1] == DEVICE_DESCRIPTOR)
             {
                 printd("device descr\r\n");
                 transfer.remaining = DEVICE_DESCRIPTOR_LENGTH;
-                transfer.ptr = deviceDesc();
+                transfer.ptr = p;
                 transfer.direction = DEVICE_TO_HOST;
                 success = true;
             }
@@ -100,16 +100,15 @@
         break;
 
     case CONFIGURATION_DESCRIPTOR:
-        if (configurationDesc() != NULL)
+        if ((p = configurationDesc()) != NULL)
         {
-            if ((configurationDesc()[0] == CONFIGURATION_DESCRIPTOR_LENGTH)
-                && (configurationDesc()[1] == CONFIGURATION_DESCRIPTOR))
+            if (p[0] == CONFIGURATION_DESCRIPTOR_LENGTH && p[1] == CONFIGURATION_DESCRIPTOR)
             {
                 printd("conf descr request\r\n");
 
                 /* Get wTotalLength */
-                transfer.remaining = configurationDesc()[2] | (configurationDesc()[3] << 8);
-                transfer.ptr = configurationDesc();
+                transfer.remaining = p[2] | (p[3] << 8);
+                transfer.ptr = p;
                 transfer.direction = DEVICE_TO_HOST;
                 success = true;
             }
@@ -122,48 +121,48 @@
         {
         case STRING_OFFSET_LANGID:
             printd("1\r\n");
-            transfer.remaining = stringLangidDesc()[0];
             transfer.ptr = stringLangidDesc();
+            transfer.remaining = transfer.ptr[0];
             transfer.direction = DEVICE_TO_HOST;
             success = true;
             break;
 
         case STRING_OFFSET_IMANUFACTURER:
             printd("2\r\n");
-            transfer.remaining =  stringImanufacturerDesc()[0];
             transfer.ptr = stringImanufacturerDesc();
+            transfer.remaining = transfer.ptr[0];
             transfer.direction = DEVICE_TO_HOST;
             success = true;
             break;
 
         case STRING_OFFSET_IPRODUCT:
             printd("3\r\n");
-            transfer.remaining = stringIproductDesc()[0];
             transfer.ptr = stringIproductDesc();
+            transfer.remaining = transfer.ptr[0];
             transfer.direction = DEVICE_TO_HOST;
             success = true;
             break;
 
         case STRING_OFFSET_ISERIAL:
             printd("4\r\n");
-            transfer.remaining = stringIserialDesc()[0];
             transfer.ptr = stringIserialDesc();
+            transfer.remaining = transfer.ptr[0];
             transfer.direction = DEVICE_TO_HOST;
             success = true;
             break;
 
         case STRING_OFFSET_ICONFIGURATION:
             printd("5\r\n");
-            transfer.remaining = stringIConfigurationDesc()[0];
             transfer.ptr = stringIConfigurationDesc();
+            transfer.remaining = transfer.ptr[0];
             transfer.direction = DEVICE_TO_HOST;
             success = true;
             break;
 
         case STRING_OFFSET_IINTERFACE:
             printd("6\r\n");
-            transfer.remaining = stringIinterfaceDesc()[0];
             transfer.ptr = stringIinterfaceDesc();
+            transfer.remaining = transfer.ptr[0];
             transfer.direction = DEVICE_TO_HOST;
             success = true;
             break;
@@ -789,22 +788,25 @@
     return realiseEndpoint(endpoint, maxPacket, RATE_FEEDBACK_MODE);
 }
 
+/*
+ *  Find a descriptor within the list of descriptors following a 
+ *  configuration descriptor
+ */
 const uint8_t *USBDevice::findDescriptor(uint8_t descriptorType, int idx)
 {
-    /* Find a descriptor within the list of descriptors */
-    /* following a configuration descriptor. */
-    uint16_t wTotalLength;
-    const uint8_t *ptr;
-
-    if (configurationDesc() == NULL)
+    /* get the start of the configuration descriptor */
+    const uint8_t *ptr = configurationDesc();
+    if (ptr == NULL)
         return NULL;
 
-    /* Check this is a configuration descriptor */
-    if ((configurationDesc()[0] != CONFIGURATION_DESCRIPTOR_LENGTH) \
-        || (configurationDesc()[1] != CONFIGURATION_DESCRIPTOR))
+    /* make sure it matches the expected length for a config descriptor */
+    if (ptr[0] != CONFIGURATION_DESCRIPTOR_LENGTH 
+        || ptr[1] != CONFIGURATION_DESCRIPTOR)
         return NULL;
 
-    wTotalLength = configurationDesc()[2] | (configurationDesc()[3] << 8);
+    /* figure the total length of the descriptor */
+    uint16_t wTotalLength = ptr[2] | (ptr[3] << 8);
+    const uint8_t *endPtr = ptr + wTotalLength;
 
     /* Check there are some more descriptors to follow */
     /* (+2 is for bLength and bDescriptorType of next descriptor) */
@@ -812,7 +814,7 @@
         return NULL;
 
     /* Start at first descriptor after the configuration descriptor */
-    ptr = &(configurationDesc()[CONFIGURATION_DESCRIPTOR_LENGTH]);
+    ptr += CONFIGURATION_DESCRIPTOR_LENGTH;
 
     /* Scan until we find the idx'th descriptor of the specified type */
     do {
@@ -828,7 +830,7 @@
 
         /* Skip to next descriptor */
         ptr += ptr[0]; /* bLength */
-    } while (ptr < (configurationDesc() + wTotalLength));
+    } while (ptr < endPtr);
 
     /* Reached end of the descriptors - not found */
     return NULL;