Skip to content

Commit

Permalink
Implement burn mode handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTheMan827 committed Nov 9, 2022
1 parent 50b4d1a commit 8fa8d7e
Showing 1 changed file with 67 additions and 4 deletions.
71 changes: 67 additions & 4 deletions WaitingFelForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace com.clusterrr.hakchi_gui
Expand Down Expand Up @@ -69,20 +70,82 @@ public WaitingFelForm()

public static bool WaitForDevice(IWin32Window owner)
{
if (Fel.DeviceExists()) return true;
if (Fel.DeviceExists())
return true;
var form = new WaitingFelForm();
form.ShowDialog(owner);
return form.DialogResult == DialogResult.OK;
}

private static bool AttemptUpdateHandshake()
{
var result = false;
using (Fel fel = new Fel())
{
var probeSuccess = false;

try
{
fel.WriteLine += (string message) => Trace.WriteLine(message);
if (!fel.Open(isFel: false))
{
throw new Exception("USB Device Not Found");
}

if (!fel.UsbUpdateProbe())
{
throw new Exception("Failed to handshake with burn mode");
}

probeSuccess = true;

if (!fel.UsbUpdateEnterFel())
{
throw new Exception("Failed to enter FEL");
}

result = true;
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
result = probeSuccess || false;
}
finally
{
fel.Close();
}
}

return result;
}

private void timer_Tick(object sender, EventArgs e)
{
if (Fel.DeviceExists())
{
DialogResult = DialogResult.OK;
timer.Enabled = false;
deviceFound = true;
Close();
var handshakeSuccess = AttemptUpdateHandshake();

new Thread(() =>
{
if (handshakeSuccess)
{
Thread.Sleep(1000);
while (!Fel.DeviceExists())
{
Thread.Sleep(1000);
}
}
Invoke(new Action(() =>
{
DialogResult = DialogResult.OK;
deviceFound = true;
Close();
}));
}).Start();
}
}

Expand Down

0 comments on commit 8fa8d7e

Please sign in to comment.