/// <summary> /// Stops all device watchers. /// </summary> private void StopDeviceWatchersInternal() { if (_deviceWatcher.Status == DeviceWatcherStatus.Started) { _deviceWatcher.Stop(); while (_deviceWatcher.Status != DeviceWatcherStatus.Stopped) { Thread.Sleep(100); } } // Clear the list of devices so we don't have potentially disconnected devices around ClearDeviceEntries(); // also clear nanoFramework devices list var devicesToRemove = _networkNanoFrameworkDevices.Select(nanoDevice => nanoDevice.DeviceId).ToList(); foreach (var deviceId in devicesToRemove) { // get device... var device = FindNanoFrameworkDevice(deviceId); // ... and remove it from collection NanoFrameworkDevices.Remove(device); device?.DebugEngine?.StopProcessing(); device?.DebugEngine?.Stop(true); } _watchersStarted = false; }
/// <summary> /// Creates a DeviceListEntry for a device and adds it to the list of devices /// </summary> /// <param name="deviceInformation">DeviceInformation on the device to be added to the list</param> /// <param name="deviceSelector">The AQS used to find this device</param> private async void AddDeviceToList(DeviceInformation deviceInformation, String deviceSelector) { // search the device list for a device with a matching interface ID var usbMatch = FindDevice(deviceInformation.Id); OnLogMessageAvailable(NanoDevicesEventSource.Log.CandidateDevice(deviceInformation.Id)); // Add the device if it's new if (usbMatch == null) { UsbDevices.Add(new UsbDeviceInformation(deviceInformation, deviceSelector)); // search the NanoFramework device list for a device with a matching interface ID var nanoFrameworkDeviceMatch = FindNanoFrameworkDevice(deviceInformation.Id); if (nanoFrameworkDeviceMatch == null) { // Create a new element for this device interface, and queue up the query of its // device information var newNanoFrameworkDevice = new NanoDevice <NanoUsbDevice>(); //newMFDevice.DeviceInformation = new UsbDeviceInformation(deviceInformation, deviceSelector); newNanoFrameworkDevice.Device.DeviceInformation = new UsbDeviceInformation(deviceInformation, deviceSelector); newNanoFrameworkDevice.Parent = this; newNanoFrameworkDevice.Transport = TransportType.Usb; // Add the new element to the end of the list of devices NanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase); // now fill in the description // try opening the device to read the descriptor if (await ConnectUsbDeviceAsync(newNanoFrameworkDevice.Device.DeviceInformation)) { // the device description format is kept to maintain backwards compatibility newNanoFrameworkDevice.Description = EventHandlerForUsbDevice.Current.DeviceInformation.Name + "_" + await GetDeviceDescriptor(5); NanoDevicesEventSource.Log.ValidDevice(newNanoFrameworkDevice.Description + " @ " + newNanoFrameworkDevice.Device.DeviceInformation.DeviceSelector); // done here, close device EventHandlerForUsbDevice.Current.CloseDevice(); } else { // couldn't open device, better remove it from the lists NanoFrameworkDevices.Remove(newNanoFrameworkDevice as NanoDeviceBase); UsbDevices.Remove(newNanoFrameworkDevice.Device.DeviceInformation); NanoDevicesEventSource.Log.QuitDevice(deviceInformation.Id); // can't do anything with this one, better dispose it newNanoFrameworkDevice.Dispose(); } } else { // this NanoFramework device is already on the list } } }
private void OnDeviceEnumerationComplete(DeviceWatcher sender, object args) { // add another device watcher completed deviceWatchersCompletedCount++; if (deviceWatchersCompletedCount == mapDeviceWatchersToDeviceSelector.Count) { // prepare a list of devices that are to be removed if they are deemed as not valid nanoFramework devices var devicesToRemove = new List <NanoDeviceBase>(); foreach (NanoDeviceBase device in NanoFrameworkDevices) { // connect to the device (as Task to get rid of the await) var connectTask = ConnectDeviceAsync(device); if (connectTask.Result) { var checkValidNFDeviceTask = CheckValidNanoFrameworkSerialDeviceAsync(); if (!checkValidNFDeviceTask.Result) { // mark this device for removal devicesToRemove.Add(device); device.StartCountdownForDispose(); } else { Debug.WriteLine($"New Serial device: {device.Description} {(((NanoDevice<NanoSerialDevice>)device).Device.DeviceInformation.DeviceInformation.Id)}"); } // done here, disconnect from the device now ((NanoDevice <NanoSerialDevice>)device).Disconnect(); } else { // couldn't open device // mark this device for removal devicesToRemove.Add(device); } } // remove device form nanoFramework device collection (force Linq to execute with call to Count()) devicesToRemove.Select(d => NanoFrameworkDevices.Remove(d)).Count(); // all watchers have completed enumeration isAllDevicesEnumerated = true; Debug.WriteLine($"Serial device enumeration completed. Found {NanoFrameworkDevices.Count} devices"); // fire event that Serial enumeration is complete OnDeviceEnumerationCompleted(); } }
private void RemoveDeviceFromList(string deviceId) { // Removes the device entry from the internal list; therefore the UI var deviceEntry = FindDevice(deviceId); NanoDevicesEventSource.Log.DeviceDeparture(deviceId); UsbDevices.Remove(deviceEntry); // get device var device = FindNanoFrameworkDevice(deviceId); // yes, remove it from collection NanoFrameworkDevices.Remove(device); device = null; }
private void OnPortNanoFrameworkDevicesOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Reset: NanoFrameworkDevices.Clear(); break; case NotifyCollectionChangedAction.Add: e.NewItems.Cast <NanoDeviceBase>().ToList().ForEach(d => NanoFrameworkDevices.Add(d)); break; case NotifyCollectionChangedAction.Remove: e.OldItems.Cast <NanoDeviceBase>().ToList().ForEach(d => NanoFrameworkDevices.Remove(d)); break; } }
private void RemoveDeviceFromList(NetworkDeviceInformation networkDevice) { // Removes the device entry from the internal list; therefore the UI var deviceEntry = FindDevice(networkDevice.DeviceId); OnLogMessageAvailable(NanoDevicesEventSource.Log.DeviceDeparture(networkDevice.DeviceId)); _networkDevices.Remove(deviceEntry); // get device... var device = FindNanoFrameworkDevice(networkDevice.DeviceId); // ... and remove it from collection NanoFrameworkDevices.Remove(device); device?.DebugEngine?.StopProcessing(); device?.DebugEngine?.Dispose(); }
private void RemoveDeviceFromList(string deviceId) { // Removes the device entry from the internal list; therefore the UI var deviceEntry = FindDevice(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; } } }); }
/// <summary> /// Creates a DeviceListEntry for a device and adds it to the list of devices /// </summary> /// <param name="deviceInformation">DeviceInformation on the device to be added to the list</param> /// <param name="deviceSelector">The AQS used to find this device</param> private async void AddDeviceToList(DeviceInformation deviceInformation, String deviceSelector) { // search the device list for a device with a matching interface ID var usbMatch = FindDevice(deviceInformation.Id); Debug.WriteLine("New USB device: " + deviceInformation.Id); // Add the device if it's new if (usbMatch == null) { UsbDevices.Add(new UsbDeviceInformation(deviceInformation, deviceSelector)); // search the NanoFramework device list for a device with a matching interface ID var nanoFrameworkDeviceMatch = FindNanoFrameworkDevice(deviceInformation.Id); if (nanoFrameworkDeviceMatch == null) { // Create a new element for this device interface, and queue up the query of its // device information var newNanoFrameworkDevice = new NanoDevice <NanoUsbDevice>(); //newMFDevice.DeviceInformation = new UsbDeviceInformation(deviceInformation, deviceSelector); newNanoFrameworkDevice.Device.DeviceInformation = new UsbDeviceInformation(deviceInformation, deviceSelector); newNanoFrameworkDevice.Parent = this; newNanoFrameworkDevice.DebugEngine = new Engine(this, newNanoFrameworkDevice); newNanoFrameworkDevice.Transport = TransportType.Usb; // Add the new element to the end of the list of devices NanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase); // now fill in the description // try opening the device to read the descriptor if (await ConnectUsbDeviceAsync(newNanoFrameworkDevice.Device.DeviceInformation).ConfigureAwait(false)) { // the device description format is kept to maintain backwards compatibility newNanoFrameworkDevice.Description = EventHandlerForUsbDevice.Current.DeviceInformation.Name + "_" + await GetDeviceDescriptor(5).ConfigureAwait(false); Debug.WriteLine("Add new nanoFramework device to list: " + newNanoFrameworkDevice.Description + " @ " + newNanoFrameworkDevice.Device.DeviceInformation.DeviceSelector); // done here, close device EventHandlerForUsbDevice.Current.CloseDevice(); } else { // couldn't open device, better remove it from the lists NanoFrameworkDevices.Remove(newNanoFrameworkDevice as NanoDeviceBase); UsbDevices.Remove(newNanoFrameworkDevice.Device.DeviceInformation); } } 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>)); } } }
/// <summary> /// Creates a DeviceListEntry for a device and adds it to the list of devices /// </summary> /// <param name="deviceInformation">DeviceInformation on the device to be added to the list</param> /// <param name="deviceSelector">The AQS used to find this device</param> private async void AddDeviceToList(DeviceInformation deviceInformation, String deviceSelector) { // search the device list for a device with a matching interface ID var serialMatch = FindDevice(deviceInformation.Id); // Add the device if it's new if (serialMatch == null) { SerialDevices.Add(new SerialDeviceInformation(deviceInformation, deviceSelector)); // search the NanoFramework device list for a device with a matching interface ID var nanoFrameworkDeviceMatch = FindNanoFrameworkDevice(deviceInformation.Id); if (nanoFrameworkDeviceMatch == null) { // Create a new element for this device interface, and queue up the query of its // device information var newNanoFrameworkDevice = new NanoDevice <NanoSerialDevice>(); newNanoFrameworkDevice.Device.DeviceInformation = new SerialDeviceInformation(deviceInformation, deviceSelector); newNanoFrameworkDevice.Parent = this; newNanoFrameworkDevice.DebugEngine = new Engine(this, newNanoFrameworkDevice); newNanoFrameworkDevice.Transport = TransportType.Serial; // Add the new element to the end of the list of devices NanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase); // perform check for valid nanoFramework device is this is not the initial enumeration if (isAllDevicesEnumerated) { // try opening the device to check for a valid nanoFramework device if (await ConnectSerialDeviceAsync(newNanoFrameworkDevice.Device.DeviceInformation).ConfigureAwait(false)) { Debug.WriteLine("New Serial device: " + deviceInformation.Id); var name = EventHandlerForSerialDevice.Current.DeviceInformation?.Properties["System.ItemNameDisplay"] as string; // acceptable names if (name == "STM32 STLink") { // now fill in the description newNanoFrameworkDevice.Description = name + " @ " + EventHandlerForSerialDevice.Current.Device.PortName; Debug.WriteLine("Add new nanoFramework device to list: " + newNanoFrameworkDevice.Description + " @ " + newNanoFrameworkDevice.Device.DeviceInformation.DeviceSelector); } // done here, close the device EventHandlerForSerialDevice.Current.CloseDevice(); return; } // couldn't open device better remove it from the collection right away NanoFrameworkDevices.Remove(newNanoFrameworkDevice as NanoDeviceBase); } } else { // this NanoFramework device is already on the list // stop the dispose countdown! nanoFrameworkDeviceMatch.StopCountdownForDispose(); } } }
/// <summary> /// Creates a DeviceListEntry for a device and adds it to the list of devices /// </summary> /// <param name="deviceInformation">DeviceInformation on the device to be added to the list</param> /// <param name="deviceSelector">The AQS used to find this device</param> private async void AddDeviceToList(DeviceInformation deviceInformation, String deviceSelector) { // search the device list for a device with a matching interface ID var serialMatch = FindDevice(deviceInformation.Id); // Add the device if it's new if (serialMatch == null) { SerialDevices.Add(new SerialDeviceInformation(deviceInformation, deviceSelector)); // search the NanoFramework device list for a device with a matching interface ID var nanoFrameworkDeviceMatch = FindNanoFrameworkDevice(deviceInformation.Id); if (nanoFrameworkDeviceMatch == null) { // Create a new element for this device interface, and queue up the query of its // device information var newNanoFrameworkDevice = new NanoDevice <NanoSerialDevice>(); newNanoFrameworkDevice.Device.DeviceInformation = new SerialDeviceInformation(deviceInformation, deviceSelector); newNanoFrameworkDevice.Parent = this; newNanoFrameworkDevice.DebugEngine = new Engine(this, newNanoFrameworkDevice); newNanoFrameworkDevice.Transport = TransportType.Serial; // Add the new element to the end of the list of devices NanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase); // perform check for valid nanoFramework device is this is not the initial enumeration if (isAllDevicesEnumerated) { // try opening the device to check for a valid nanoFramework device if (await ConnectSerialDeviceAsync(newNanoFrameworkDevice.Device.DeviceInformation)) { Debug.WriteLine("New Serial device: " + deviceInformation.Id); if (await CheckValidNanoFrameworkSerialDeviceAsync()) { // done here, close the device EventHandlerForSerialDevice.Current.CloseDevice(); // remove it from collection... NanoFrameworkDevices.Remove(newNanoFrameworkDevice as NanoDeviceBase); //... and add it again, otherwise the bindings might fail NanoFrameworkDevices.Add(newNanoFrameworkDevice as NanoDeviceBase); Debug.WriteLine("Add new nanoFramework device to list: " + newNanoFrameworkDevice.Description + " @ " + newNanoFrameworkDevice.Device.DeviceInformation.DeviceSelector); return; } } Debug.WriteLine($"Removing { deviceInformation.Id } from collection..."); // couldn't open device better remove it from the collection right away NanoFrameworkDevices.Remove(newNanoFrameworkDevice as NanoDeviceBase); // can't do anything with this one, better dispose it newNanoFrameworkDevice.Dispose(); } } else { // this NanoFramework device is already on the list // stop the dispose countdown! nanoFrameworkDeviceMatch.StopCountdownForDispose(); } } }