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(); }
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); } } }
public DeviceEventArgs(ResponseDevice d, DeviceEventType e) { Device = d; Event = e; Deny = false; }