private void IOnUsbNotify(object sender, DeviceNotifyEventArgs e) { DeviceID id = new DeviceID(e.Device); // Do we give any shits about this device? if (!DeviceFactory.Contains(id.Vendor, id.Product)) { return; } // Wazzup? switch (e.EventType) { case EventType.DeviceArrival: { IInitDevice(id, DeviceEventType.DeviceAdded); break; } case EventType.DeviceQueryRemove: { ResponseDevice device = m_devices[id]; bool canRemove = device.IBeginSoftRemoval(); /// TODO: deny this request break; } case EventType.DeviceRemoveComplete: { IDestroyDevice(id); break; } } }
private void IDestroyDevice(DeviceID id) { ResponseDevice device = m_devices[id]; DeviceRemoved?.Invoke(this, new DeviceEventArgs(device, DeviceEventType.DeviceRemoved)); m_devices.Remove(id); device.IDeviceRemoved(); }
internal static ResponseDevice Create(DeviceID device) { Check(); Tuple <int, int> key = new Tuple <int, int>(device.Vendor, device.Product); if (!s_devices.ContainsKey(key)) { return(null); } Type imp = s_devices[key]; return((ResponseDevice)Activator.CreateInstance(imp)); }
public void BeginDiscovery() { if (m_usbNotifier != null) { throw new InvalidOperationException(); } // Test all the loaded HID devices to see if they're valid HidDeviceLoader loader = new HidDeviceLoader(); foreach (var device in loader.GetDevices()) { DeviceID id = new DeviceID(device); IInitDevice(id, DeviceEventType.DeviceDiscovered); } // Register for new device insertions m_usbNotifier = DeviceNotifier.OpenDeviceNotifier(); m_usbNotifier.OnDeviceNotify += IOnUsbNotify; }
private async void IInitDevice(DeviceID id, DeviceEventType e, HidDevice hid = null) { ResponseDevice device = DeviceFactory.Create(id); if (device != null) { m_log.Trace("ResponseDevice detected..."); if (hid == null) { HidDeviceLoader loader = new HidDeviceLoader(); for (int i = 0; i < 10; i++) { hid = loader.GetDeviceOrDefault(vendorID: id.Vendor, productID: id.Product); if (hid == null) { m_log.Trace("... HID device matching USB insert not found, retrying in 50ms"); Thread.Sleep(50); // :( } else { break; } } if (hid == null) { m_log.Warn("Could not find HID device [V:{0:X}] [P:{1:X}] [S:{2}] after USB insert", id.Vendor, id.Product, id.Serial); return; } } m_log.Info("Discovered `{0}` [V:{1:X}] [P:{2:X}] [S:{3}]", hid.ProductName, id.Vendor, id.Product, id.Serial); if (await device.Initialize(hid)) { m_devices.Add(id, device); DeviceDiscovered?.Invoke(this, new DeviceEventArgs(device, e)); } else { m_log.Warn("Failed to init `{0}` [V:{1:X}] [P:{2:X}] [S:{3}]", hid.ProductName, id.Vendor, id.Product, id.Serial); } } }