Просмотр исходного кода

add descriptor string count for tud_desc_set_t

hathach 7 лет назад
Родитель
Сommit
262be103e0
3 измененных файлов с 19 добавлено и 11 удалено
  1. 3 1
      examples/device/nrf52840/src/tusb_descriptors.c
  2. 11 7
      src/device/usbd.c
  3. 5 3
      src/device/usbd.h

+ 3 - 1
examples/device/nrf52840/src/tusb_descriptors.c

@@ -85,7 +85,9 @@ tud_desc_set_t tud_desc_set =
 {
     .device     = NULL,
     .config     = NULL,
-    .string_arr = (uint8_t const **) string_desc_arr,
+
+    .string_arr   = (uint8_t const **) string_desc_arr,
+    .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
 
     .hid_report =
     {

+ 11 - 7
src/device/usbd.c

@@ -315,7 +315,7 @@ static void usbd_reset(uint8_t rhport)
   extern tusb_desc_device_t const _desc_auto_device;
   extern uint8_t const * const    _desc_auto_config;
 
-  tud_desc_set.device = &_desc_auto_device;
+  tud_desc_set.device = (uint8_t const*) &_desc_auto_device;
   tud_desc_set.config = _desc_auto_config;
 
 #if CFG_TUD_HID_BOOT_PROTOCOL
@@ -509,13 +509,17 @@ static uint16_t get_descriptor(uint8_t rhport, tusb_control_request_t const * co
     break;
 
     case TUSB_DESC_STRING:
-      // windows sometimes ask for string at index 238 !!!
-      if ( !(desc_index < 100) ) return 0;
-
-      desc_data = tud_desc_set.string_arr[desc_index];
-      VERIFY( desc_data != NULL, 0 );
+      if ( desc_index < tud_desc_set.string_count )
+      {
+        desc_data = tud_desc_set.string_arr[desc_index];
+        VERIFY( desc_data != NULL, 0 );
 
-      len  = desc_data[0];  // first byte of descriptor is its size
+        len  = desc_data[0];  // first byte of descriptor is its size
+      }else
+      {
+        // out of range
+        return 0;
+      }
     break;
 
     case TUSB_DESC_DEVICE_QUALIFIER:

+ 5 - 3
src/device/usbd.h

@@ -59,9 +59,11 @@
 
 /// \brief Descriptor pointer collector to all the needed.
 typedef struct {
-  void const * device;     ///< pointer to device descriptor \ref tusb_desc_device_t
-  uint8_t const * config;     ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
-  uint8_t const** string_arr; ///< a array of pointers to string descriptors
+  uint8_t const * device;            ///< pointer to device descriptor \ref tusb_desc_device_t
+  uint8_t const * config;         ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
+
+  uint8_t const** string_arr;     ///< a array of pointers to string descriptors
+  uint16_t        string_count;
 
   struct {
     uint8_t const* composite;