From 941b02c6a98cd34eb84cf499db9810108871d49b Mon Sep 17 00:00:00 2001
From: Mengsk <admin@hifiphile.com>
Date: Wed, 7 Jul 2021 18:02:04 +0200
Subject: [PATCH] Reactor to one functional descriptor.

---
 examples/device/dfu/src/tusb_config.h     |  8 ++--
 examples/device/dfu/src/usb_descriptors.c |  7 ++--
 src/class/dfu/dfu_device.c                | 51 ++++++++++-------------
 src/class/dfu/dfu_device.h                |  7 +---
 src/device/usbd.h                         | 47 +++++++++++++++++----
 5 files changed, 72 insertions(+), 48 deletions(-)

diff --git a/examples/device/dfu/src/tusb_config.h b/examples/device/dfu/src/tusb_config.h
index 0df46c14e..55aa19922 100644
--- a/examples/device/dfu/src/tusb_config.h
+++ b/examples/device/dfu/src/tusb_config.h
@@ -76,14 +76,16 @@
 #define CFG_TUD_ENDPOINT0_SIZE    64
 #endif
 
-// DFU buffer size, must to be set to the largest buffer size used by an any given storage type
-#define CFG_TUD_DFU_TRANSFER_BUFFER_SIZE    4096
-
 //------------- CLASS -------------//
 
 #define CFG_TUD_DFU_RUNTIME 0
 #define CFG_TUD_DFU_MODE    1
 
+// Count of all alt settings, typically it's the partition count (Flash, EEPROM, etc.)
+#define CFG_TUD_DFU_ALT_COUNT               2
+// DFU buffer size, it has to be set to the buffer size used in TUD_DFU_MODE_DESCRIPTOR
+#define CFG_TUD_DFU_TRANSFER_BUFFER_SIZE    4096
+
 #ifdef __cplusplus
  }
 #endif
diff --git a/examples/device/dfu/src/usb_descriptors.c b/examples/device/dfu/src/usb_descriptors.c
index 9565da9b2..200817eb4 100644
--- a/examples/device/dfu/src/usb_descriptors.c
+++ b/examples/device/dfu/src/usb_descriptors.c
@@ -87,7 +87,7 @@ enum
   ITF_NUM_TOTAL
 };
 
-#define CONFIG_TOTAL_LEN    (TUD_CONFIG_DESC_LEN + 2 * TUD_DFU_MODE_DESC_LEN)
+#define CONFIG_TOTAL_LEN    (TUD_CONFIG_DESC_LEN + TUD_DFU_MODE_DESC_LEN)
 
 #define FUNC_ATTRS (DFU_FUNC_ATTR_CAN_UPLOAD_BITMASK | DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK)
 
@@ -96,9 +96,8 @@ uint8_t const desc_configuration[] =
   // Config number, interface count, string index, total length, attribute, power in mA
   TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
 
-  // Interface number, string index, attributes, detach timeout, transfer size */
-  TUD_DFU_MODE_DESCRIPTOR(ITF_NUM_DFU_MODE, 0, 4, FUNC_ATTRS, 1000, CFG_TUD_DFU_TRANSFER_BUFFER_SIZE),
-  TUD_DFU_MODE_DESCRIPTOR(ITF_NUM_DFU_MODE, 1, 5, FUNC_ATTRS, 1000, CFG_TUD_DFU_TRANSFER_BUFFER_SIZE),
+  // Interface number, detach timeout, transfer size, string index 0, [string index 1 ... string index n]
+  TUD_DFU_MODE_DESCRIPTOR(ITF_NUM_DFU_MODE, 0, FUNC_ATTRS, 1000, CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, 4, 5),
 };
 
 // Invoked when received GET CONFIGURATION DESCRIPTOR
diff --git a/src/class/dfu/dfu_device.c b/src/class/dfu/dfu_device.c
index 8bf2e16be..834648c5a 100644
--- a/src/class/dfu/dfu_device.c
+++ b/src/class/dfu/dfu_device.c
@@ -46,7 +46,7 @@ typedef struct TU_ATTR_PACKED
 {
     dfu_device_status_t status;
     dfu_state_t state;
-    uint8_t attrs[CFG_TUD_DFU_ATL_MAX];
+    uint8_t attrs;
     bool blk_transfer_in_proc;
     uint8_t alt;
     uint8_t intf;
@@ -148,10 +148,7 @@ void dfu_moded_init(void)
 {
   _dfu_state_ctx.state = DFU_IDLE;
   _dfu_state_ctx.status = DFU_STATUS_OK;
-  for (uint8_t i = 0; i < CFG_TUD_DFU_ATL_MAX; i++)
-  {
-    _dfu_state_ctx.attrs[i] = 0;
-  }
+  _dfu_state_ctx.attrs = 0;
   _dfu_state_ctx.blk_transfer_in_proc = false;
   _dfu_state_ctx.alt = 0;
   _dfu_state_ctx.intf = DFU_INTF_UNUSED;
@@ -165,10 +162,7 @@ void dfu_moded_reset(uint8_t rhport)
 
   _dfu_state_ctx.state = DFU_IDLE;
   _dfu_state_ctx.status = DFU_STATUS_OK;
-  for (uint8_t i = 0; i < CFG_TUD_DFU_ATL_MAX; i++)
-  {
-    _dfu_state_ctx.attrs[i] = 0;
-  }
+  _dfu_state_ctx.attrs = 0;
   _dfu_state_ctx.blk_transfer_in_proc = false;
   _dfu_state_ctx.alt = 0;
   _dfu_state_ctx.intf = DFU_INTF_UNUSED;
@@ -180,15 +174,15 @@ uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
 {
   (void) rhport;
 
-  uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_dfu_functional_t);
+  uint16_t const drv_len = sizeof(tusb_desc_interface_t);
 
   uint8_t const *p_desc = (uint8_t const *)itf_desc;
 
   uint16_t total_len = 0;
-  
+
   uint8_t last_alt = 0;
 
-  while(max_len >= drv_len)
+  while(max_len > drv_len)
   {
     // Ensure this is DFU Mode
     TU_VERIFY((((tusb_desc_interface_t const *)p_desc)->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS) &&
@@ -208,25 +202,26 @@ uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
     }
 
     // CFG_TUD_DFU_ATL_MAX should big enough to hold all alt settings
-    TU_ASSERT(alt < CFG_TUD_DFU_ATL_MAX, 0);
+    TU_ASSERT(alt < CFG_TUD_DFU_ALT_COUNT, 0);
 
     // Alt should increse by one every time
     TU_ASSERT(alt == last_alt++, 0);
 
-    //------------- DFU descriptor -------------//
-    p_desc = tu_desc_next(p_desc);
-    TU_ASSERT(tu_desc_type(p_desc) == TUSB_DESC_FUNCTIONAL, 0);
-
-    _dfu_state_ctx.attrs[alt] = ((tusb_desc_dfu_functional_t const *)p_desc)->bAttributes;
-
-    // CFG_TUD_DFU_TRANSFER_BUFFER_SIZE has to be set to the largest buffer size used by all alt settings
-    TU_ASSERT(((tusb_desc_dfu_functional_t const *)p_desc)->wTransferSize <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, 0);
-
     p_desc = tu_desc_next(p_desc);
     max_len -= drv_len;
     total_len += drv_len;
   }
 
+  //------------- DFU descriptor -------------//
+  TU_ASSERT(tu_desc_type(p_desc) == TUSB_DESC_FUNCTIONAL, 0);
+
+  _dfu_state_ctx.attrs = ((tusb_desc_dfu_functional_t const *)p_desc)->bAttributes;
+
+  // CFG_TUD_DFU_TRANSFER_BUFFER_SIZE has to be set to the buffer size used in TUD_DFU_MODE_DESCRIPTOR
+  TU_ASSERT(((tusb_desc_dfu_functional_t const *)p_desc)->wTransferSize <= CFG_TUD_DFU_TRANSFER_BUFFER_SIZE, 0);
+
+  total_len += sizeof(tusb_desc_dfu_functional_t); 
+
   return total_len;
 }
 
@@ -272,7 +267,7 @@ bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_reque
     case DFU_REQUEST_DNLOAD:
     {
       if ( (stage == CONTROL_STAGE_ACK)
-           && ((_dfu_state_ctx.attrs[_dfu_state_ctx.alt] & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
+           && ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
            && (_dfu_state_ctx.state == DFU_DNLOAD_SYNC))
       {
         dfu_req_dnload_reply(rhport, request);
@@ -358,7 +353,7 @@ void tud_dfu_dnload_complete(void)
     _dfu_state_ctx.state = DFU_DNLOAD_SYNC;
   } else if (_dfu_state_ctx.state == DFU_MANIFEST)
   {
-    _dfu_state_ctx.state = ((_dfu_state_ctx.attrs[_dfu_state_ctx.alt] & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
+    _dfu_state_ctx.state = ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
                            ? DFU_MANIFEST_WAIT_RESET : DFU_MANIFEST_SYNC;
   }
 }
@@ -376,7 +371,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
       {
         case DFU_REQUEST_DNLOAD:
         {
-          if( ((_dfu_state_ctx.attrs[_dfu_state_ctx.alt] & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
+          if( ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
               && (request->wLength > 0) )
           {
             _dfu_state_ctx.state = DFU_DNLOAD_SYNC;
@@ -390,7 +385,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
 
         case DFU_REQUEST_UPLOAD:
         {
-          if( ((_dfu_state_ctx.attrs[_dfu_state_ctx.alt] & DFU_FUNC_ATTR_CAN_UPLOAD_BITMASK) != 0) )
+          if( ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_UPLOAD_BITMASK) != 0) )
           {
             _dfu_state_ctx.state = DFU_UPLOAD_IDLE;
             dfu_req_upload(rhport, request, request->wValue, request->wLength);
@@ -481,7 +476,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
         {
           case DFU_REQUEST_DNLOAD:
           {
-            if( ((_dfu_state_ctx.attrs[_dfu_state_ctx.alt] & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
+            if( ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_CAN_DOWNLOAD_BITMASK) != 0)
                 && (request->wLength > 0) )
             {
               _dfu_state_ctx.state = DFU_DNLOAD_SYNC;
@@ -538,7 +533,7 @@ static bool dfu_state_machine(uint8_t rhport, tusb_control_request_t const * req
       {
         case DFU_REQUEST_GETSTATUS:
         {
-          if ((_dfu_state_ctx.attrs[_dfu_state_ctx.alt] & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
+          if ((_dfu_state_ctx.attrs & DFU_FUNC_ATTR_MANIFESTATION_TOLERANT_BITMASK) == 0)
           {
             _dfu_state_ctx.state = DFU_MANIFEST;
             dfu_req_getstatus_reply(rhport, request);
diff --git a/src/class/dfu/dfu_device.h b/src/class/dfu/dfu_device.h
index 53be0005e..f41b4b651 100644
--- a/src/class/dfu/dfu_device.h
+++ b/src/class/dfu/dfu_device.h
@@ -37,13 +37,8 @@
 // Class Driver Default Configure & Validation
 //--------------------------------------------------------------------+
 
-// Maximum alternate settings (used for different partitons) supported
-#if !defined(CFG_TUD_DFU_ATL_MAX)
-  #define  CFG_TUD_DFU_ATL_MAX  2
-#endif
-
 #if !defined(CFG_TUD_DFU_TRANSFER_BUFFER_SIZE)
-  #error "CFG_TUD_DFU_TRANSFER_BUFFER_SIZE must be defined, it has to be set to the largest buffer size used by an any given storage type"
+  #error "CFG_TUD_DFU_TRANSFER_BUFFER_SIZE must be defined, it has to be set to the buffer size used in TUD_DFU_MODE_DESCRIPTOR"
 #endif
 
 //--------------------------------------------------------------------+
diff --git a/src/device/usbd.h b/src/device/usbd.h
index 41c78de10..fe1863706 100644
--- a/src/device/usbd.h
+++ b/src/device/usbd.h
@@ -600,17 +600,50 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
   /* Function */ \
   9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101)
 
-// Length of template descriptr: 18 bytes
-#define TUD_DFU_MODE_DESC_LEN (9 + 9)
+// Maximum alternate settings (used for different partitons) supported
+#ifndef CFG_TUD_DFU_ALT_COUNT
+#define CFG_TUD_DFU_ALT_COUNT 1
+#endif
 
-// DFU mode descriptor
-// Interface number, alt settings, string index, attributes, detach timeout, transfer size
-#define TUD_DFU_MODE_DESCRIPTOR(_itfnum, _alt, _stridx, _attr, _timeout, _xfer_size) \
-  /* Interface */ \
-  9, TUSB_DESC_INTERFACE, _itfnum, _alt, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_DFU, _stridx, \
+// Length of template descriptor: 18 bytes + number of alternatives * 9
+#define TUD_DFU_MODE_DESC_LEN (9 + (CFG_TUD_DFU_ALT_COUNT) * 9)
+
+/* Primary Interface */
+#define TUD_DFU_MODE_FUNC(_attr, _timeout, _xfer_size) \
   /* Function */ \
   9, DFU_DESC_FUNCTIONAL, _attr, U16_TO_U8S_LE(_timeout), U16_TO_U8S_LE(_xfer_size), U16_TO_U8S_LE(0x0101)
 
+#define TUD_DFU_MODE_ALT(_itfnum, _alt, _stridx) \
+  /* Interface */ \
+  9, TUSB_DESC_INTERFACE, _itfnum, _alt, 0, TUD_DFU_APP_CLASS, TUD_DFU_APP_SUBCLASS, DFU_PROTOCOL_DFU, _stridx, \
+
+#define _TUD_DFU_FIRST(a, ...) a
+#define _TUD_DFU_REST(a, ...) __VA_ARGS__
+#define _TUD_DFU_COMBINE(...) __VA_ARGS__
+
+#define TUD_DFU_MODE_ALT_1(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 1, _TUD_DFU_FIRST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_2(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 2, _TUD_DFU_FIRST(__VA_ARGS__)) \
+  TUD_DFU_MODE_ALT_1(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_3(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 3, _TUD_DFU_FIRST(__VA_ARGS__)) \
+  TUD_DFU_MODE_ALT_2(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_4(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 4, _TUD_DFU_FIRST(__VA_ARGS__)) \
+  TUD_DFU_MODE_ALT_3(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_5(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 5, _TUD_DFU_FIRST(__VA_ARGS__)) \
+  TUD_DFU_MODE_ALT_4(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_6(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 6, _TUD_DFU_FIRST(__VA_ARGS__)) \
+  TUD_DFU_MODE_ALT_5(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_7(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 7, _TUD_DFU_FIRST(__VA_ARGS__)) \
+  TUD_DFU_MODE_ALT_6(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+#define TUD_DFU_MODE_ALT_8(_itfnum, ...) TUD_DFU_MODE_ALT(_itfnum, (CFG_TUD_DFU_ALT_COUNT) - 8, _TUD_DFU_FIRST(__VA_ARGS__)) \
+  TUD_DFU_MODE_ALT_7(_itfnum, _TUD_DFU_REST(__VA_ARGS__))
+
+#define TUD_DFU_MODE_ALTS(_itfnum, ...) \
+  TU_XSTRCAT(TUD_DFU_MODE_ALT_, CFG_TUD_DFU_ALT_COUNT)(_itfnum, __VA_ARGS__)
+
+// Interface number, detach timeout, transfer size, string index 1, [string index 2, string index n]
+#define TUD_DFU_MODE_DESCRIPTOR(_itfnum, _attr, _timeout, _xfer_size, _stridx, ...) \
+  TUD_DFU_MODE_ALTS(_itfnum, _TUD_DFU_COMBINE(_stridx, __VA_ARGS__)) \
+  TUD_DFU_MODE_FUNC(_attr, _timeout, _xfer_size)
 
 //------------- CDC-ECM -------------//