Skip to content

Commit 0484fc7

Browse files
committed
Dfu: restructure Manifest to avoid code duplication
Signed-off-by: Benedek Kupper <kupper.benedek@gmail.com>
1 parent d95e314 commit 0484fc7

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

Dfu/Device.cs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -309,57 +309,55 @@ public void ResetToIdle()
309309
/// </summary>
310310
public void Manifest()
311311
{
312-
Status status;
313-
314312
// After the zero length DFU_DNLOAD request terminates the Transfer phase,
315313
// the device is ready to manifest the new firmware.
316314
Dnload(0, new byte[0]);
317315

318-
// If the device enters dfuMANIFEST-SYNC (bitMainfestationTolerant = 1),
319-
// then the host issues the DFU_GETSTATUS request, and the device enters the dfuIDLE state.
320-
// At that point, the host can perform another download, solicit an upload, or issue a USB reset
321-
// to return the device to application run-time mode.
322-
if (DfuDescriptor.ManifestationTolerant)
316+
try
323317
{
318+
// wait until manifesting completes
319+
Status status;
324320
for (status = GetStatus(); status.State == State.Manifest; status = GetStatus())
325321
{
326322
Thread.Sleep(status.PollTimeout);
327323
}
328324

329-
VerifyState(status, State.Idle);
330-
331-
BusReset();
332-
}
333-
// If, however, the device enters the dfuMANIFEST-WAIT-RESET state (bitManifestationTolerant = 0),
334-
// then if bitWillDetach = 1 the device generates a detach-attach sequence on the bus,
335-
// otherwise (bitWillDetach = 0) the host must issue a USB reset to the device.
336-
// After the bus reset the device will evaluate the firmware status and enter the appropriate mode.
337-
else
338-
{
339-
try
325+
if (DfuDescriptor.ManifestationTolerant)
340326
{
341-
for (status = GetStatus(); status.State == State.Manifest; status = GetStatus())
342-
{
343-
Thread.Sleep(status.PollTimeout);
344-
}
345-
346-
VerifyState(status, State.ManifestWaitReset);
327+
// If the device enters dfuMANIFEST-SYNC (bitMainfestationTolerant = 1),
328+
// then the host issues the DFU_GETSTATUS request, and the device enters the dfuIDLE state.
329+
VerifyState(status, State.Idle);
347330

331+
// At that point, the host can perform another download, solicit an upload, or issue a USB reset
332+
// to return the device to application run-time mode.
348333
BusReset();
349334
}
350-
catch (Exception)
335+
else
351336
{
352-
// exceptions due to broken pipe are allowed if the USB device detaches itself
353-
if (DfuDescriptor.WillDetach)
354-
{
355-
}
356-
else
337+
// If, however, the device enters the dfuMANIFEST-WAIT-RESET state (bitManifestationTolerant = 0),
338+
// then if bitWillDetach = 1 the device generates a detach-attach sequence on the bus,
339+
VerifyState(status, State.ManifestWaitReset);
340+
341+
// otherwise (bitWillDetach = 0) the host must issue a USB reset to the device.
342+
// After the bus reset the device will evaluate the firmware status and enter the appropriate mode.
343+
if (!DfuDescriptor.WillDetach)
357344
{
358-
throw;
345+
BusReset();
359346
}
360347
}
361-
Close();
362348
}
349+
catch (Exception)
350+
{
351+
// exceptions due to broken pipe are allowed if the USB device detaches itself
352+
if (!DfuDescriptor.ManifestationTolerant && DfuDescriptor.WillDetach)
353+
{
354+
}
355+
else
356+
{
357+
throw;
358+
}
359+
}
360+
Close();
363361
}
364362

365363
/// <summary>

0 commit comments

Comments
 (0)