dfu: Introduce CONFIG_BOOT_WAIT_FOR_USB_DFU

This Kconfig parameter makes MCUboot wait for a prescribed duration
of time to allow for USB DFU to be invoked.

Signed-off-by: Rajavardhan Gundi <rajavardhan.gundi@intel.com>
diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig
index 77c5b63..29193d1 100644
--- a/boot/zephyr/Kconfig
+++ b/boot/zephyr/Kconfig
@@ -122,6 +122,17 @@
 	 on some hardware that has long erase times, to prevent long wait
 	 times at the beginning of the DFU process.
 
+config BOOT_WAIT_FOR_USB_DFU
+	bool "Wait for a prescribed duration to see if USB DFU is invoked"
+	default n
+	select USB
+	select USB_DFU_CLASS
+	select IMG_MANAGER
+	help
+	  If y, MCUboot waits for a prescribed duration of time to allow
+	  for USB DFU to be invoked. Please note DFU always updates the
+	  slot1 image.
+
 config ZEPHYR_TRY_MASS_ERASE
 	bool "Try to mass erase flash when flashing MCUboot image"
 	default y
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index 236f80c..e276454 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -40,6 +40,10 @@
 };
 #endif
 
+#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
+#include <usb/class/usb_dfu.h>
+#endif
+
 MCUBOOT_LOG_MODULE_REGISTER(mcuboot);
 
 void os_heap_init(void);
@@ -197,6 +201,12 @@
     }
 #endif
 
+#ifdef CONFIG_BOOT_WAIT_FOR_USB_DFU
+    BOOT_LOG_INF("Waiting for USB DFU");
+    wait_for_usb_dfu();
+    BOOT_LOG_INF("USB DFU wait time elapsed");
+#endif
+
     rc = boot_go(&rsp);
     if (rc != 0) {
         BOOT_LOG_ERR("Unable to find bootable image");