Skip to content

Commit

Permalink
Rework device watcher (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes authored Oct 20, 2017
1 parent 608b9cb commit 2de82a0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 118 deletions.
1 change: 0 additions & 1 deletion source/USB Test App WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ private async void PingButton_Click(object sender, RoutedEventArgs e)

// enable button
(sender as Button).IsEnabled = true;

}

private object await(MainViewModel mainViewModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ public NanoDevice()
{
Transport = TransportType.Usb;
}

SuicideTimer = new Timer((state) =>
{
Task.Factory.StartNew(() =>
{
// set kill flag
KillFlag = true;
DebugEngine.Dispose();
Dispose(false);
});
}, null, Timeout.Infinite, Timeout.Infinite);
}

#region Disposable implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public abstract class NanoDeviceBase
/// </summary>
public INanoFrameworkDeviceInfo DeviceInfo { get; internal set; }

public bool KillFlag { get; protected set; } = false;

// timer to "suicide" NanoFramework device after the hardware instance has been removed or detected as inactive by the port where it was connected
protected Timer SuicideTimer;

private object m_serverCert = null;
private Dictionary<uint, string> m_execSrecHash = new Dictionary<uint, string>();
private Dictionary<uint, int> m_srecHash = new Dictionary<uint, int>();
Expand Down Expand Up @@ -83,31 +78,6 @@ private bool IsClrDebuggerEnabled

public object OnProgress { get; private set; }

/// <summary>
/// Start count down to dispose NanoFramework device. The dispose will occur after 2 seconds.
/// </summary>
public void StartCountdownForDispose()
{
StartCountdownForDispose(TimeSpan.FromSeconds(2));
}

/// <summary>
/// Start count down to dispose NanoFramework device. The dispose will occur after the timeToDispose argument is elapsed.
/// </summary>
/// <param name="timeToDispose">Time to wait before the device is disposed</param>
public void StartCountdownForDispose(TimeSpan timeToDispose)
{
SuicideTimer.Change(timeToDispose, TimeSpan.FromMilliseconds(-1));
}

/// <summary>
/// Stop the dispose countdown.
/// </summary>
public void StopCountdownForDispose()
{
SuicideTimer.Change(Timeout.Infinite, Timeout.Infinite);
}

/// <summary>
/// Get <see cref="INanoFrameworkDeviceInfo"/> from device.
/// If the device information has been retrieved before this method returns the cached data, unless the force argument is true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,6 @@ private async void AddDeviceToList(DeviceInformation deviceInformation, String d
newNanoFrameworkDevice.Dispose();
}
}
else
{
// this NanoFramework device is already on the list
// stop the dispose countdown!
nanoFrameworkDeviceMatch.StopCountdownForDispose();
}
}
}

Expand All @@ -296,32 +290,50 @@ private void RemoveDeviceFromList(string deviceId)

SerialDevices.Remove(deviceEntry);

// start thread to dispose and remove device from collection if it doesn't enumerate again in 2 seconds
Task.Factory.StartNew(() =>
{
// get device
var device = FindNanoFrameworkDevice(deviceId);
// get device
var device = FindNanoFrameworkDevice(deviceId);
// yes, remove it from collection
NanoFrameworkDevices.Remove(device);

if (device != null)
{
// set device to dispose if it doesn't come back in 2 seconds
device.StartCountdownForDispose();
device = null;

// hold here for the same time as the default timer of the dispose timer
new ManualResetEvent(false).WaitOne(TimeSpan.FromSeconds(2.5));
//// start thread to dispose and remove device from collection if it doesn't enumerate again in 2 seconds
//Task.Factory.StartNew(() =>
//{
// // get device
// var device = FindNanoFrameworkDevice(deviceId);

// check is object was disposed
if ((device as NanoDevice<NanoSerialDevice>).KillFlag)
{
// yes, remove it from collection
NanoFrameworkDevices.Remove(device);
// if (device != null)
// {
// // set device to dispose if it doesn't come back in 2.5 seconds
// device.StartCountdownForDispose();

Debug.WriteLine("Removing device " + device.Description);
// // hold here for the same time as the default timer of the dispose timer
// new ManualResetEvent(false).WaitOne(TimeSpan.FromSeconds(4));

device = null;
}
}
});

// // try to find device
// var newDevice = FindNanoFrameworkDevice(deviceId);


// // check is object was disposed
// if ((newDevice as NanoDevice<NanoSerialDevice>).KillFlag)
// {
// // yes, remove it from collection
// NanoFrameworkDevices.Remove(newDevice);

// Debug.WriteLine("Removing device " + newDevice.Description);

// newDevice = null;
// }
// else
// {
// // add it again to serial devices collection
// deviceEntry = FindDevice(deviceId);
// SerialDevices.Add(deviceEntry);
// }
// }
//});
}

private void ClearDeviceEntries()
Expand Down Expand Up @@ -411,7 +423,6 @@ private void OnDeviceEnumerationComplete(DeviceWatcher sender, object args)
{
// mark this device for removal
devicesToRemove.Add(device);
device.StartCountdownForDispose();
}
else
{
Expand Down
39 changes: 5 additions & 34 deletions source/nanoFramework.Tools.DebugLibrary.Shared/PortUSB/UsbPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,6 @@ private async void AddDeviceToList(DeviceInformation deviceInformation, String d
else
{
// this NanoFramework device is already on the list
// stop the dispose countdown!
nanoFrameworkDeviceMatch.StopCountdownForDispose();

// set port parent
//(mfDeviceMatch as MFDevice<MFUsbDevice>).Device.Parent = this;
//// instantiate a debug engine
//(mfDeviceMatch as MFDevice<MFUsbDevice>).Device.DebugEngine = new Engine(this, (mfDeviceMatch as MFDevice<MFUsbDevice>));
}
}
}
Expand All @@ -276,33 +269,11 @@ private void RemoveDeviceFromList(string deviceId)
Debug.WriteLine("USB device removed: " + deviceId);

UsbDevices.Remove(deviceEntry);

// start thread to dispose and remove device from collection if it doesn't enumerate again in 2 seconds
Task.Factory.StartNew(() =>
{
// get device
var device = FindNanoFrameworkDevice(deviceId);
if (device != null)
{
// set device to dispose if it doesn't come back in 2 seconds
device.StartCountdownForDispose();
// hold here for the same time as the default timer of the dispose timer
new ManualResetEvent(false).WaitOne(TimeSpan.FromSeconds(2.5));
// check is object was disposed
if((device as NanoDevice<NanoUsbDevice>).KillFlag)
{
// yes, remove it from collection
NanoFrameworkDevices.Remove(device);
Debug.WriteLine("Removing device " + device.Description);
device = null;
}
}
});
// get device
var device = FindNanoFrameworkDevice(deviceId);
// yes, remove it from collection
NanoFrameworkDevices.Remove(device);
device = null;
}

private void ClearDeviceEntries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,23 +693,16 @@ public async Task RebootDeviceAsync(RebootOption option = RebootOption.NormalReb

await PerformRequestAsync(Commands.c_Monitor_Reboot, Flags.c_NoCaching, cmd, 0, 100);

if (option != RebootOption.NoReconnect)
// need to disconnect from the device if this is normal reboot
if ((cmd.m_flags & Commands.Monitor_Reboot.c_NormalReboot) == Commands.Monitor_Reboot.c_NormalReboot)
{
//int timeout = 1000;

//if (m_portDefinition is PortDefinition_Tcp)
//{
// timeout = 2000;
//}

//Thread.Sleep(timeout);
Device.Disconnect();
}
}
finally
{
m_fThrowOnCommunicationFailure = fThrowOnCommunicationFailureSav;
}

}

public async Task<bool> ReconnectAsync(bool fSoftReboot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public enum RebootOption
EnterBootloader,
RebootClrOnly,
NormalReboot,
NoReconnect,
RebootClrWaitForDebugger,
};
}

0 comments on commit 2de82a0

Please sign in to comment.